【问题标题】:Distance between two geo-points?两个地理点之间的距离?
【发布时间】:2011-02-03 12:40:07
【问题描述】:

如何在给定两个地理点(两个纬度/经度对)的情况下获得准确的距离(以米为单位)?

可能的重复:

Distance Between Two GEO Locations

Calculating the distance of geo locations

Android calculate distance between two locations

How to find distance from the latitude and longitude of two locations?

【问题讨论】:

标签: iphone latitude-longitude


【解决方案1】:

iPhone 上没有距离测量仪可以为您提供 2 米的分辨率。您可以使用 Core Location 的 -[CLLocation distanceFromLocation: otherLocation] 方法获取两个位置之间的位移(以米为单位),但请记住:

  • 在我所见的任何地方,Apple 都没有解释过它们的坐标使用什么 geode,以及实际上对于不同的位置计算它是否是同一个 geode
  • 他们使用的模型没有考虑高度,这对于计算场地大小区域中人类大小物体之间的距离非常糟糕。不过,它可以计算伦敦和莫斯科之间的距离 - 误差很小。
  • 当您的设备未插入电源时,使用真正高精度的位置数据与运动检测相结合会完全耗尽电池电量
  • 不使用移动侦测,你只能告诉within tens of metres设备在哪里。

【讨论】:

    【解决方案2】:

    如果你想得到两个坐标的距离,你可以使用这个 sn-p:

    #include <math.h>
    #define DEG2RAD(degrees) (degrees * 0.01745327)
    #define RADIUS_OF_EARTH 6378.1
    
    + (float) getDistanceFromStartCoords:(CLLocationCoordinate2D)start andEndCoords:(CLLocationCoordinate2D)end 
    {
        float dist = acos((cos(DEG2RAD(start.latitude))*
                     cos(DEG2RAD(end.latitude))*
                     cos((-1*DEG2RAD(end.longitude))-
                         (-1*DEG2RAD(start.longitude)))) +
                  (sin(DEG2RAD(start.latitude))*
                   sin(DEG2RAD(end.latitude)))) * 
                RADIUS_OF_EARTH;
    
        return dist;
    }
    

    【讨论】:

    • 世界不是球形的。您相对于大地水准面的位置推算误差高达 21 公里。
    • @Graham 正确...但是 Manish Jain 不会在两米的距离上检查点!!!两米之内,世界导火索不变!
    • 但我在 CLLocation 变量中的距离不在 CLLocationCoordinate2D 中
    • CLLocationCoordinate2D 开始;起始纬度 = 28.5344643;起始经度 = 77.2778694; CLLocationCoordinate2D 最终; final.latitude = 27.5344643; final.longitude = 76.2778694;
    • 我已经使用了这个并传入了函数但显示了不可兼容的指针错误
    【解决方案3】:

    这是对上述解决方案的“改进”。它添加了海拔信息。苹果返回的高度似乎以米为单位。不适合飞行或轨道等,但如果有人在另一个人的正上方 15 层,在附近的山上等,则可以工作。未经广泛测试。它假设您不关心 20 公里以外的高度。然后,当您离对方更近时,它会提供高度校正。因此,对于相距 20 米但高 100m 的两个人,您的距离约为 102 米。最后我切换到km返回。还在原代码中发现了一个nan bug。

    #define DEG2RAD(degrees) (degrees * 0.01745329251)
    #define RADIUS_OF_EARTH 6371000.0
    // km
    + (double)getDistanceFromStartCoords:(CLLocationCoordinate2D)start altStart:(double)altStart andEndCoords:(CLLocationCoordinate2D)end altEnd:(double)altEnd;
    {
        double argument = (cos(DEG2RAD(start.latitude))*
                     cos(DEG2RAD(end.latitude))*
                     cos((-1*DEG2RAD(end.longitude))-
                         (-1*DEG2RAD(start.longitude)))) +
                  (sin(DEG2RAD(start.latitude))*
                   sin(DEG2RAD(end.latitude)));
    
        double dist = 0.0;
        if (argument < 1.0 && argument > -1.0) // acos will return nan for very small (0) distance
            dist = acos(argument)*RADIUS_OF_EARTH;
    //    else
    //        NSLog(@"found bug, %f", acos(argument));
    
    
        // Altitude hack.
        // blend in an altitude correction (blend for smoothness)
        // add in altitude difference
        double altDiff = fabs(altStart - altEnd); // altdiff
        double factor = 1.0 - dist/20000.0;
        if (factor < 0.0)
            factor = 0.0;
    
        dist += sqrt(dist*dist + factor*altDiff*altDiff);
    
        //NSLog(@"distance found, %f", dist);
        return dist/1000.0; // return km
    }
    

    【讨论】:

      猜你喜欢
      • 2011-02-14
      • 2013-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-02
      • 1970-01-01
      • 1970-01-01
      • 2011-12-20
      相关资源
      最近更新 更多