相关疑难解决方法(0)

计算两个CLLocationCoordinate2D之间的方位

非常"简单"的问题:给定两个CLLocationCoordinate2Ds,如何从第一个到第二个获得方位(以弧度为单位)?我已经做了很多研究和研究,特别是一般问题和Objective-C/Cocoa Touch/iOS.

这是我的实现:

- (float) getHeadingForDirectionFromCoordinate:(CLLocationCoordinate2D)fromLoc toCoordinate:(CLLocationCoordinate2D)toLoc
{
    float fLat = fromLoc.latitude;
    float fLng = fromLoc.longitude;
    float tLat = toLoc.latitude;
    float tLng = toLoc.longitude;

    return atan2(sin(fLng-tLng)*cos(tLat), cos(fLat)*sin(tLat)-sin(fLat)*cos(tLat)*cos(fLng-tLng));         
}
Run Code Online (Sandbox Code Playgroud)

但是,这种方法并没有为我返回一致的结果.如果轴承接近正北或正南,看起来很好,但是,任何其他方向似乎都会返回不一致的数据,例如:

从50.405018,8.437500

至51.339802,12.403340

我的方法返回:5.918441弧度

应该是1.18660576弧度

(见http://www.movable-type.co.uk/scripts/latlong.htmlhttp://www.movable-type.co.uk/scripts/latlong-map.html?lat1=50.405018&long1=8.437500 &lat2 = 51.339802&long2 = 12.403340)

我已经双重和三重检查公式是正确的.我也发现了一些像上面的例子一样的值,有些是正确的,有些是错的.我玩过各种模数或返回值的界限,也没有运气.

有任何想法吗?我的代码有问题吗?也许我误解了数学函数是如何工作的?

objective-c core-location coordinates ios

38
推荐指数
3
解决办法
2万
查看次数

标签 统计

coordinates ×1

core-location ×1

ios ×1

objective-c ×1