使用指南针指向位置
我正在尝试为应用程序开发一个指南针,该应用程序在地图上有一组注释。我希望能够选择注释,然后让指南针将用户指向所选位置。
我计算了从用户位置到注释位置的度数,并且获得了 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 旋转?
I am trying to develop a compass for an appliation which has a set of annotations on a map. I would like to be able to choose an annotation and then have the compass point the user to the chosen location.
I have calculated the degress from the user's location to the location of the annotation and I have the magnetic heading and the true heading of the iPhone. Also I know how to rotate an image.
But I can't figure out what the next step is.
The degrees between the user's location and the annotation location is calculated like this:
// 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;
}
The true and the magnetic heading is retrieved like this:
- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading {
arrow.transform = CGAffineTransformMakeRotation(newHeading.magneticHeading);
// NSLog(@"magnetic heading: %f", newHeading.magneticHeading);
// NSLog(@"true heading: %f", newHeading.trueHeading);
}
Can anyone tell me what I should do now to make the arrow point to the location - even if the iPhone is rotated?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我有时间再玩这个。
这将做到这一点:
I had time to play with this again.
This will do it: