【发布时间】:2011-05-26 14:16:19
【问题描述】:
如何计算 iPhone 地图应用程序上两个 POI(兴趣点)坐标之间的角度(以度为单位)?
【问题讨论】:
-
什么是 POI,什么是“iPhone amp 应用程序”?
-
@Radek S:@Jano 更新原始问题后应该有意义
如何计算 iPhone 地图应用程序上两个 POI(兴趣点)坐标之间的角度(以度为单位)?
【问题讨论】:
我猜你试图计算两个兴趣点 (POI) 坐标之间的度数。
计算great circle的弧:
+(float) greatCircleFrom:(CLLocation*)first
to:(CLLocation*)second {
int radius = 6371; // 6371km is the radius of the earth
float dLat = second.coordinate.latitude-first.coordinate.latitude;
float dLon = second.coordinate.longitude-first.coordinate.longitude;
float a = pow(sin(dLat/2),2) + cos(first.coordinate.latitude)*cos(second.coordinate.latitude) * pow(sin(dLon/2),2);
float c = 2 * atan2(sqrt(a),sqrt(1-a));
float d = radius * c;
return d;
}
另一种选择是假装你在笛卡尔坐标上(在长距离上更快但并非没有错误):
+(float)angleFromCoordinate:(CLLocationCoordinate2D)first
toCoordinate:(CLLocationCoordinate2D)second {
float deltaLongitude = second.longitude - first.longitude;
float deltaLatitude = second.latitude - first.latitude;
float angle = (M_PI * .5f) - atan(deltaLatitude / deltaLongitude);
if (deltaLongitude > 0) return angle;
else if (deltaLongitude < 0) return angle + M_PI;
else if (deltaLatitude < 0) return M_PI;
return 0.0f;
}
如果您想要以度数而不是弧度为单位的结果,则必须应用以下转换:
#define RADIANS_TO_DEGREES(radians) ((radians) * 180.0 / M_PI)
【讨论】:
您在这里计算从一点到另一点的“方位”。在这个网页上有一大堆公式,以及许多其他地理量,如距离和交叉轨道误差:
http://www.movable-type.co.uk/scripts/latlong.html
这些公式有多种格式,因此您可以轻松地将其转换为您的 iPhone 所需的任何语言。还有 javascript 计算器,因此您可以测试您的代码得到与他们相同的答案。
【讨论】:
如果其他解决方案不适合你,试试这个:
- (int)getInitialBearingFrom:(CLLocation *)first
to:(CLLocation *)second
{
float lat1 = [self degreesToRad:first.coordinate.latitude];
float lat2 = [self degreesToRad:second.coordinate.latitude];
float lon1 = [self degreesToRad:first.coordinate.longitude];
float lon2 = [self degreesToRad:second.coordinate.longitude];
float dLon = lon2 - lon1;
float y = sin (dLon) * cos (lat2);
float x1 = cos (lat1) * sin (lat2);
float x2 = sin (lat1) * cos (lat2) * cos (dLon);
float x = x1 - x2;
float bearingRadRaw = atan2f (y, x);
float bearingDegRaw = bearingRadRaw * 180 / M_PI;
int bearing = ((int) bearingDegRaw + 360) % 360; // +- 180 deg to 360 deg
return bearing;
}
对于最终方位,只需将初始方位从终点取到起点并将其反转(使用 θ = (θ+180) % 360)。
你需要这 2 个助手:
-(float)radToDegrees:(float)radians
{
return radians * 180 / M_PI;
}
-(float)degreesToRad:(float)degrees
{
return degrees * M_PI /180;
}
【讨论】: