使用Compass指向实际坐标位置而不是仅指向North

时间:2011-02-28 17:38:52

标签: iphone compass-geolocation direction heading bearing

我已经看到了一些这样的例子,但我不确定如何实现它,(例如iPhone Compass GPS Direction)我需要在(void)启动中调用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 个答案:

答案 0 :(得分:2)

我会找到从坐标A到坐标B的轴承,然后使用该轴承作为从坐标A到坐标B的罗盘方位的偏移。

以下是两个用于查找坐标轴承的链接

Calculating bearing between two CLLocationCoordinate2Ds

http://shawnsbits.com/blog/2011/07/07/calculating-heading-with-corelocation/

如果您希望罗盘轴承在移动时更新,因此它始终指向坐标B,那么您可能希望在获得新轴承或位置时重新计算。