如何获得两个POI之间的角度? [英] How to get angle between two POI?

查看:152
本文介绍了如何获得两个POI之间的角度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How do I calculate the angle in degrees between the coordinates of two POIs (points of interest) on an iPhone map application?

推荐答案

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

I'm guessing you try to calculate the degrees between the coordinates of two points of interest (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;
}

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

Another option is to pretend you are on cartesian coordinates (faster but not without error on long distances):

+(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;
}

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

If you want the result in degrees instead radians, you have to apply the following conversion:

#define RADIANS_TO_DEGREES(radians) ((radians) * 180.0 / M_PI)

这篇关于如何获得两个POI之间的角度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆