Using Compass to point to actual coordinate location instead of just North

我只是一个虾纸丫 提交于 2019-12-21 20:59:25

问题


I have seen a few examples of this but am not sure exactly how to implement it, (such as iPhone Compass GPS Direction) in do I need to call calcBearingWithLatitude in (void)startup?

All I am trying to do is have the compass pull a point from memory and use that point as its "north" so to speak and point to it, no need for distance or anything like that.

My implementation file as of now looks like

@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:


I would find the bearing from coordinate A to coordinate B and then use that bearing as an offset for the compass bearing from coordinate A to coordinate B.

Here are two links for finding a bearing to a coordinate

Calculating bearing between two CLLocationCoordinate2Ds

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

If you want the compass bearing to update as you move so it always points to coordinate B, then you probably want to recalculate whenever you get a new bearing or location.



来源:https://stackoverflow.com/questions/5145465/using-compass-to-point-to-actual-coordinate-location-instead-of-just-north

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!