使用指南针指向实际坐标位置,而不仅仅是北
Posted
技术标签:
【中文标题】使用指南针指向实际坐标位置,而不仅仅是北【英文标题】:Using Compass to point to actual coordinate location instead of just North 【发布时间】:2011-07-05 22:51:19 【问题描述】:我已经看到了一些这样的例子,但不确定如何实现它,(例如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
【问题讨论】:
你能为指南针创建一个自定义标题吗? 【参考方案1】:我会找到从坐标 A 到坐标 B 的方位,然后使用该方位作为罗盘方位从坐标 A 到坐标 B 的偏移量。
这里有两个链接可以找到坐标的方位
Calculating bearing between two CLLocationCoordinate2Ds
http://shawnsbits.com/blog/2011/07/07/calculating-heading-with-corelocation/
如果您希望罗盘方位在您移动时更新,使其始终指向坐标 B,那么您可能需要在获得新方位或位置时重新计算。
【讨论】:
以上是关于使用指南针指向实际坐标位置,而不仅仅是北的主要内容,如果未能解决你的问题,请参考以下文章