CLLocation 标头始终为 -1
嘿伙计们,我正在尝试构建一个应用程序,它将使用指南针指向预定位置的方向,但现在我正在尝试了解如何制作基本指南针。我遵循了指南,但是我总是得到“-1”作为标题,并且只让它工作一次,从那以后我就没有更改过代码。任何想法都会有帮助。
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
@interface TrackerViewController : UIViewController <CLLocationManagerDelegate> {
}
@property (nonatomic, retain) CLLocationManager *locationManager;
@property (nonatomic, retain) IBOutlet UISwitch *toggle;
@property (nonatomic, retain) IBOutlet UIImageView *compass;
@property (nonatomic, retain) IBOutlet UILabel *headingLabel;
-(IBAction)toggleSwitch;
@end
以及实施文件...
#import "TrackerViewController.h"
@implementation TrackerViewController
@synthesize locationManager;
@synthesize toggle;
@synthesize compass;
@synthesize headingLabel;
- (IBAction)toggleSwitch {
if(self.toggle.isOn) {
[self.locationManager startUpdatingHeading];
} else {
[self.locationManager stopUpdatingHeading];
}
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
if ([CLLocationManager headingAvailable]) {
self.locationManager = [[[CLLocationManager alloc] init] autorelease];
self.locationManager.delegate = self;
} else {
[[[[UIAlertView alloc] initWithTitle:@"Aw snap!"
message:@"Device doesn't support heading updates"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil] autorelease] show];
}
}
- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading
*)newHeading {
float heading = [newHeading trueHeading] * M_PI / 180.0;
self.compass.transform = CGAffineTransformMakeRotation(-heading);
self.headingLabel.text = [NSString stringWithFormat:@"%d°", (int)[newHeading trueHeading]];
NSLog(@"%d", (int)[newHeading trueHeading]);
}
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error {
// NSLog(@"Error!");
if (error.code == kCLErrorDenied) {
[[[[UIAlertView alloc] initWithTitle:@"Aw snap!"
message:@"User didn't allow heading updates"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil] autorelease] show];
self.toggle.on = NO;
[self.locationManager stopUpdatingHeading];
}
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
self.locationManager = nil;
self.compass = nil;
self.toggle = nil;
self.headingLabel = nil;
[super dealloc];
}
@end
Hey guys, I am trying to build an app that will use a compass to point in the direction of a predetermined location, but for now I am trying to understand just how to make a basic compass. I followed a guide, however I am always get "-1" as the header, and have only gotten it to work once, and I haven't changed code since. Any ideas would be helpful.
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
@interface TrackerViewController : UIViewController <CLLocationManagerDelegate> {
}
@property (nonatomic, retain) CLLocationManager *locationManager;
@property (nonatomic, retain) IBOutlet UISwitch *toggle;
@property (nonatomic, retain) IBOutlet UIImageView *compass;
@property (nonatomic, retain) IBOutlet UILabel *headingLabel;
-(IBAction)toggleSwitch;
@end
and the implementation file...
#import "TrackerViewController.h"
@implementation TrackerViewController
@synthesize locationManager;
@synthesize toggle;
@synthesize compass;
@synthesize headingLabel;
- (IBAction)toggleSwitch {
if(self.toggle.isOn) {
[self.locationManager startUpdatingHeading];
} else {
[self.locationManager stopUpdatingHeading];
}
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
if ([CLLocationManager headingAvailable]) {
self.locationManager = [[[CLLocationManager alloc] init] autorelease];
self.locationManager.delegate = self;
} else {
[[[[UIAlertView alloc] initWithTitle:@"Aw snap!"
message:@"Device doesn't support heading updates"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil] autorelease] show];
}
}
- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading
*)newHeading {
float heading = [newHeading trueHeading] * M_PI / 180.0;
self.compass.transform = CGAffineTransformMakeRotation(-heading);
self.headingLabel.text = [NSString stringWithFormat:@"%d°", (int)[newHeading trueHeading]];
NSLog(@"%d", (int)[newHeading trueHeading]);
}
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error {
// NSLog(@"Error!");
if (error.code == kCLErrorDenied) {
[[[[UIAlertView alloc] initWithTitle:@"Aw snap!"
message:@"User didn't allow heading updates"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil] autorelease] show];
self.toggle.on = NO;
[self.locationManager stopUpdatingHeading];
}
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
self.locationManager = nil;
self.compass = nil;
self.toggle = nil;
self.headingLabel = nil;
[super dealloc];
}
@end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
磁航向
该属性中的值表示相对于磁北极的航向,这与地理北极不同。值 0 表示设备指向磁北,90 表示设备指向东,180 表示设备指向南,依此类推。此属性中的值应始终有效。
magneticHeading
The value in this property represents the heading relative to the magnetic North Pole, which is different from the geographic North Pole. The value 0 means the device is pointed toward magnetic north, 90 means it is pointed east, 180 means it is pointed south, and so on. The value in this property should always be valid.