我正在尝试为在地图上有一组注释的应用程序开发指南针。我希望能够选择一个注释,然后让指南针将用户指向所选位置。
我已经计算了从用户位置到注释位置的距离,我得到了 iPhone 的磁航向和真实航向。我也知道如何旋转图像。但我不知道下一步是什么。
用户位置和注释位置之间的度数计算如下:
// get user location
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.distanceFilter = kCLDistanceFilterNone;
[locationManager startUpdatingLocation];
[locationManager startUpdatingHeading];
CLLocation *location = [locationManager location];
CLLocationCoordinate2D coordinate = [location coordinate];
float x1 = coordinate.latitude;
float y1 = coordinate.longitude;
float x2 = [annLatitude floatValue];
float y2 = [annLongitude floatValue];
float dx = (x2 - x1);
float dy = (y2 - y1);
if (dx == 0) {
if (dy > 0) {
result = 90;
}
else {
result = 270;
}
}
else {
result = (atan(dy/dx)) * 180 / M_PI;
}
if (dx < 0) {
result = result + 180;
}
if (result < 0) {
result = result + 360;
}
真航向和磁航向检索如下:
- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading {
arrow.transform = CGAffineTransformMakeRotation(newHeading.magneticHeading);
// NSLog(@"magnetic heading: %f", newHeading.magneticHeading);
// NSLog(@"true heading: %f", newHeading.trueHeading);
}
谁能告诉我我现在应该怎么做才能使箭头指向该位置 - 即使 iPhone 旋转了?