我见过一些这样的例子,但不确定到底如何实现它,(例如iPhone 指南针 GPS 方向 https://stackoverflow.com/questions/4130821/iphone-compass-gps-direction)中我需要在 (void)startup 中调用 calcBearingWithLatitude 吗?
我想做的就是让指南针从记忆中提取一个点,并使用该点作为它的“北”,可以这么说并指向它,不需要距离或类似的东西。
我的实现文件现在看起来像
@implementation TrackerViewController
@synthesize locationManager;
@synthesize compass;
@synthesize headingLabel;
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
if ([CLLocationManager headingAvailable]) {
} else {
[[[[UIAlertView alloc] initWithTitle:@"Error"
message:@"Device doesn't support heading updates"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil] autorelease] show];
}
}
- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading {
double heading = [newHeading magneticHeading] * M_PI / 180.0;
self.compass.transform = CGAffineTransformMakeRotation(-heading);
self.headingLabel.text = [NSString stringWithFormat:@"%d°", (int)[newHeading magneticHeading]];
NSLog(@"%d", (int)[newHeading magneticHeading]);
}
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error {
// NSLog(@"Error!");
if (error.code == kCLErrorDenied) {
[[[[UIAlertView alloc] initWithTitle:@"Error"
message:@"User didn't allow heading updates"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil] autorelease] show];
[self.locationManager stopUpdatingHeading];
}
}
- (void)viewDidLoad {
locationManager=[[CLLocationManager alloc] init];
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.delegate=self;
//Start the compass updates.
[locationManager startUpdatingHeading];
[super viewDidLoad];
}
- (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)dealloc {
self.locationManager = nil;
self.compass = nil;
self.headingLabel = nil;
[super dealloc];
}
@end
我会找到从坐标 A 到坐标 B 的方位,然后使用该方位作为罗盘方位从坐标 A 到坐标 B 的偏移。
以下是查找坐标方位的两个链接
计算两个 CLLocationCooperative2D 之间的方位角 https://stackoverflow.com/questions/3809337/calculating-bearing-between-two-cllocationcoordinate2ds
http://shawnsbits.com/blog/2011/07/07/calculate-heading-with-corelocation/ http://shawnsbits.com/blog/2011/07/07/calculating-heading-with-corelocation/
如果您希望罗盘方位在您移动时更新,以便它始终指向坐标 B,那么您可能需要在获得新方位或位置时重新计算。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)