如何获得两个POI之间的角度?

mr.*_*bor 3 iphone map

如何计算iPhone地图应用程序中两个POI(兴趣点)坐标之间的角度?

Jan*_*ano 12

我猜你试图计算两个兴趣点(POI)坐标之间的度数.

计算大圆弧:

+(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;
}
Run Code Online (Sandbox Code Playgroud)

另一个选择是假装你是笛卡尔坐标(更快但不是没有长距离误差):

+(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;
}
Run Code Online (Sandbox Code Playgroud)

如果您希望结果以度为弧度,则必须应用以下转换:

#define RADIANS_TO_DEGREES(radians) ((radians) * 180.0 / M_PI)
Run Code Online (Sandbox Code Playgroud)