【问题标题】:Get distance between 2 latitude & longitude in iOS6+在iOS6+中获取2个纬度和经度之间的距离
【发布时间】:2013-07-30 07:06:51
【问题描述】:

我想计算 2 lat & long 之间的距离。我可以计算距离如下

CLLocation *currentLoc = [[CLLocation alloc] initWithLatitude:-24.4132995 longitude:121.0790024];
CLLocation *restaurnatLoc = [[CLLocation alloc] initWithLatitude:-32.8310013 longitude:150.1390075];
CLLocationDistance meters = [restaurnatLoc distanceFromLocation:currentLoc];
NSLog(@"Distance between 2 geo cordinates: %.2f Meters",meters);

现在我想获取从 currentLocation 到restaurnatLoc 的方向。为此,我有以下代码

double DegreesToRadians(double degrees) {return degrees * M_PI / 180;};
double RadiansToDegrees(double radians) {return radians * 180/M_PI;};

-(double) bearingToLocationFromCoordinate:(CLLocation*)fromLoc toCoordinate:(CLLocation*)toLoc
{
    double lat1 = DegreesToRadians(fromLoc.coordinate.latitude);
    double lon1 = DegreesToRadians(fromLoc.coordinate.longitude);
    
    double lat2 = DegreesToRadians(toLoc.coordinate.latitude);
    double lon2 = DegreesToRadians(toLoc.coordinate.longitude);
    
    double dLon = lon2 - lon1;
    
    double y = sin(dLon) * cos(lat2);
    double x = cos(lat1) * sin(lat2) - sin(lat1) * cos(lat2) * cos(dLon);
    double radiansBearing = atan2(y, x);
    
    return RadiansToDegrees(radiansBearing);
}

它返回bearing = 114.975752 现在我如何确定餐厅是否在我当前位置的North、South、West、East、NW、NE、SW、SE?

我从这个链接Direction based off of 2 Lat,Long points得到1个解决方案但是如果我考虑这个解决方案,那么我怀疑从我的位置(红色圆圈)到餐厅(绿色圆圈)的轴承114,如下所示。如果我错了,请纠正我。

由于当前位置是“西澳大利亚”,餐厅位置是“悉尼”,如 Google 地图所示。

任何人都可以告诉我这里出了什么问题吗?谢谢。

////////////////////////更新 ///////// //////////////////

我的指南针图是错误的。感谢 AlexWien,这是正确的图表

现在我得到了正确的输出

【问题讨论】:

    标签: ios gps cllocation direction bearing


    【解决方案1】:

    你的罗盘完全错了。你看过指南针吗?打开 iphone 指南针应用程序并查看 90Degrees 所在的位置。它是东方,而不是您的图形中的西方。

    地理方向是顺时针测量的!

    所以 114 度是东方,符合您的预期

    【讨论】:

    • 我的错...非常抱歉。实际上我没有 iPhone,我正在开发 iPod touch。谢谢你指点我。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-02
    • 1970-01-01
    • 1970-01-01
    • 2013-10-25
    • 1970-01-01
    相关资源
    最近更新 更多