【问题标题】:Calculating Distance between two Latitude and Longitude GeoCoordinates计算两个纬度和经度坐标之间的距离
【发布时间】:2011-09-16 00:31:29
【问题描述】:

我正在计算两个地理坐标之间的距离。我正在针对 3-4 个其他应用程序测试我的应用程序。当我计算距离时,我的计算平均距离为 3.3 英里,而其他应用程序则为 3.5 英里。这对我尝试执行的计算有很大的不同。有没有好的类库来计算距离?我在 C# 中是这样计算的:

public static double Calculate(double sLatitude,double sLongitude, double eLatitude, 
                               double eLongitude)
{
    var radiansOverDegrees = (Math.PI / 180.0);

    var sLatitudeRadians = sLatitude * radiansOverDegrees;
    var sLongitudeRadians = sLongitude * radiansOverDegrees;
    var eLatitudeRadians = eLatitude * radiansOverDegrees;
    var eLongitudeRadians = eLongitude * radiansOverDegrees;

    var dLongitude = eLongitudeRadians - sLongitudeRadians;
    var dLatitude = eLatitudeRadians - sLatitudeRadians;

    var result1 = Math.Pow(Math.Sin(dLatitude / 2.0), 2.0) + 
                  Math.Cos(sLatitudeRadians) * Math.Cos(eLatitudeRadians) * 
                  Math.Pow(Math.Sin(dLongitude / 2.0), 2.0);

    // Using 3956 as the number of miles around the earth
    var result2 = 3956.0 * 2.0 * 
                  Math.Atan2(Math.Sqrt(result1), Math.Sqrt(1.0 - result1));

    return result2;
}

我可能做错了什么?我应该先以公里计算,然后再转换为英里吗?

【问题讨论】:

  • 地球平均半径 = 6,371km = 3958.76 英里
  • 不应该在 gis.stackexchange.com 上
  • 它可能有,但我的问题更多地涉及在 Windows Phone 上计算它,这有点不同。公式是一样的,但是像 DistanceTo 方法这样的新方法调用不一定可用。
  • 建议您存储 pi/180 这样您就不必不断重复计算。

标签: c# windows-phone-7 geocoding latitude-longitude


【解决方案1】:

在这里,对于那些仍然不满意的人(比如我),来自 .NET-Frameworks GeoCoordinate 类的原始代码被重构为一个独立的方法:

public double GetDistance(double longitude, double latitude, double otherLongitude, double otherLatitude)
{
    var d1 = latitude * (Math.PI / 180.0);
    var num1 = longitude * (Math.PI / 180.0);
    var d2 = otherLatitude * (Math.PI / 180.0);
    var num2 = otherLongitude * (Math.PI / 180.0) - num1;
    var d3 = Math.Pow(Math.Sin((d2 - d1) / 2.0), 2.0) + Math.Cos(d1) * Math.Cos(d2) * Math.Pow(Math.Sin(num2 / 2.0), 2.0);
    
    return 6376500.0 * (2.0 * Math.Atan2(Math.Sqrt(d3), Math.Sqrt(1.0 - d3)));
}

【讨论】:

  • 美丽的答案,我想指出,由此产生的距离以米为单位。如official documentation所述
  • 谢谢!我正在寻找 GeoCoordinate 类中使用的实际地球半径。
  • 小优化,或者为了便于阅读,可以预先计算 pi/180 double oneDegree = Math.PI / 180.0; ?
  • @brakeroo 感谢您的回复。我想保留原样,因为这是原始的 .NET 代码。当然,任何人都可以随意遵循您的建议。
  • 工作就像一个魅力,最简单的答案;)
【解决方案2】:

当 CPU/数学计算能力有限时:

有时(例如在我的工作中)计算能力不足(例如,没有浮点处理器,使用小型微控制器),某些三角函数可能会占用过多的 CPU 时间(例如 3000 多个时钟周期),所以当我只需要一个近似值时,特别是如果 CPU 不能长时间占用,我用它来最小化 CPU 开销:

/**------------------------------------------------------------------------
 * \brief  Great Circle distance approximation in km over short distances.
 *
 * Can be off by as much as 10%.
 *
 * approx_distance_in_mi = sqrt(x * x + y * y)
 *
 * where x = 69.1 * (lat2 - lat1)
 * and y = 69.1 * (lon2 - lon1) * cos(lat1/57.3)
 *//*----------------------------------------------------------------------*/
double    ApproximateDisatanceBetweenTwoLatLonsInKm(
                  double lat1, double lon1,
                  double lat2, double lon2
                  ) {
    double  ldRadians, ldCosR, x, y;

    ldRadians = (lat1 / 57.3) * 0.017453292519943295769236907684886;
    ldCosR = cos(ldRadians);
    x = 69.1 * (lat2 - lat1);
    y = 69.1 * (lon2 - lon1) * ldCosR;

    return sqrt(x * x + y * y) * 1.609344;  /* Converts mi to km. */
}

归功于https://github.com/kristianmandrup/geo_vectors/blob/master/Distance%20calc%20notes.txt

【讨论】:

    【解决方案3】:

    基于 Elliot Wood 的函数,如果有人对 C 函数感兴趣,这个函数正在工作......

    #define SIM_Degree_to_Radian(x) ((float)x * 0.017453292F)
    #define SIM_PI_VALUE                         (3.14159265359)
    
    float GPS_Distance(float lat1, float lon1, float lat2, float lon2)
    {
       float theta;
       float dist;
    
       theta = lon1 - lon2;
    
       lat1 = SIM_Degree_to_Radian(lat1);
       lat2 = SIM_Degree_to_Radian(lat2);
       theta = SIM_Degree_to_Radian(theta);
    
       dist = (sin(lat1) * sin(lat2)) + (cos(lat1) * cos(lat2) * cos(theta));
       dist = acos(dist);
    
    //   dist = dist * 180.0 / SIM_PI_VALUE;
    //   dist = dist * 60.0 * 1.1515;
    //   /* Convert to km */
    //   dist = dist * 1.609344;
    
       dist *= 6370.693486F;
    
       return (dist);
    }
    

    您可以将其更改为 double。它返回以公里为单位的值。

    【讨论】:

      【解决方案4】:

      这是一个老问题,但在性能和优化方面,答案并不让我满意。

      这是我优化的 C# 变体(距离以公里为单位,没有变量和冗余计算,非常接近 Haversine Formular https://en.wikipedia.org/wiki/Haversine_formula 的数学表达式)。

      灵感来自: https://rosettacode.org/wiki/Haversine_formula#C.23

      public static class Haversine
      {
          public static double Calculate(double lat1, double lon1, double lat2, double lon2)
          {
              double rad(double angle) => angle * 0.017453292519943295769236907684886127d; // = angle * Math.Pi / 180.0d
              double havf(double diff) => Math.Pow(Math.Sin(rad(diff) / 2d), 2); // = sin²(diff / 2)
              return 12745.6 * Math.Asin(Math.Sqrt(havf(lat2 - lat1) + Math.Cos(rad(lat1)) * Math.Cos(rad(lat2)) * havf(lon2 - lon1))); // earth radius 6.372,8‬km x 2 = 12745.6
          }
      }
      

      【讨论】:

        【解决方案5】:

        这些平台有这个库GeoCoordinate

        • 单声道
        • .NET 4.5
        • .NET 核心
        • Windows Phone 8.x
        • 通用 Windows 平台
        • Xamarin iOS
        • Xamarin Android

        通过 NuGet 完成安装:

        PM> 安装包地理坐标

        用法

        GeoCoordinate pin1 = new GeoCoordinate(lat, lng);
        GeoCoordinate pin2 = new GeoCoordinate(lat, lng);
        
        double distanceBetween = pin1.GetDistanceTo(pin2);
        

        两个坐标之间的距离,以为单位。

        【讨论】:

          【解决方案6】:

          你可以使用这个功能:

          来源:https://www.geodatasource.com/developers/c-sharp

          private double distance(double lat1, double lon1, double lat2, double lon2, char unit) {
            if ((lat1 == lat2) && (lon1 == lon2)) {
              return 0;
            }
            else {
              double theta = lon1 - lon2;
              double dist = Math.Sin(deg2rad(lat1)) * Math.Sin(deg2rad(lat2)) + Math.Cos(deg2rad(lat1)) * Math.Cos(deg2rad(lat2)) * Math.Cos(deg2rad(theta));
              dist = Math.Acos(dist);
              dist = rad2deg(dist);
              dist = dist * 60 * 1.1515;
              if (unit == 'K') {
                dist = dist * 1.609344;
              } else if (unit == 'N') {
                dist = dist * 0.8684;
              }
              return (dist);
            }
          }
          
          //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
          //::  This function converts decimal degrees to radians             :::
          //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
          private double deg2rad(double deg) {
            return (deg * Math.PI / 180.0);
          }
          
          //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
          //::  This function converts radians to decimal degrees             :::
          //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
          private double rad2deg(double rad) {
            return (rad / Math.PI * 180.0);
          }
          
          Console.WriteLine(distance(32.9697, -96.80322, 29.46786, -98.53506, "M"));
          Console.WriteLine(distance(32.9697, -96.80322, 29.46786, -98.53506, "K"));
          Console.WriteLine(distance(32.9697, -96.80322, 29.46786, -98.53506, "N"));
          

          【讨论】:

          • 完美运行!谢谢!
          【解决方案7】:

          对于那些使用 Xamarin 并且无法访问 GeoCoordinate 类的用户,您可以改用 Android Location 类:

          public static double GetDistanceBetweenCoordinates (double lat1, double lng1, double lat2, double lng2) {
                      var coords1 = new Location ("");
                      coords1.Latitude = lat1;
                      coords1.Longitude = lng1;
                      var coords2 = new Location ("");
                      coords2.Latitude = lat2;
                      coords2.Longitude = lng2;
                      return coords1.DistanceTo (coords2);
                  }
          

          【讨论】:

            【解决方案8】:

            你可以使用System.device.Location:

            System.device.Location.GeoCoordinate gc = new System.device.Location.GeoCoordinate(){
            Latitude = yourLatitudePt1,
            Longitude = yourLongitudePt1
            };
            
            System.device.Location.GeoCoordinate gc2 = new System.device.Location.GeoCoordinate(){
            Latitude = yourLatitudePt2,
            Longitude = yourLongitudePt2
            };
            
            Double distance = gc2.getDistanceTo(gc);
            

            祝你好运

            【讨论】:

              【解决方案9】:

              GetDistance 是最好的解决方案,但在很多情况下我们不能使用这种方法(例如 Universal App)

              • 伪代码算法的计算距离到坐标:

                public static double DistanceTo(double lat1, double lon1, double lat2, double lon2, char unit = 'K')
                {
                    double rlat1 = Math.PI*lat1/180;
                    double rlat2 = Math.PI*lat2/180;
                    double theta = lon1 - lon2;
                    double rtheta = Math.PI*theta/180;
                    double dist =
                        Math.Sin(rlat1)*Math.Sin(rlat2) + Math.Cos(rlat1)*
                        Math.Cos(rlat2)*Math.Cos(rtheta);
                    dist = Math.Acos(dist);
                    dist = dist*180/Math.PI;
                    dist = dist*60*1.1515;
                
                    switch (unit)
                    {
                        case 'K': //Kilometers -> default
                            return dist*1.609344;
                        case 'N': //Nautical Miles 
                            return dist*0.8684;
                        case 'M': //Miles
                            return dist;
                    }
                
                    return dist;
                }
                
              • 真实世界的 C# 实现,它使用 扩展方法

                用法:

                var distance = new Coordinates(48.672309, 15.695585)
                                .DistanceTo(
                                    new Coordinates(48.237867, 16.389477),
                                    UnitOfLength.Kilometers
                                );
                

                实施:

                public class Coordinates
                {
                    public double Latitude { get; private set; }
                    public double Longitude { get; private set; }
                
                    public Coordinates(double latitude, double longitude)
                    {
                        Latitude = latitude;
                        Longitude = longitude;
                    }
                }
                public static class CoordinatesDistanceExtensions
                {
                    public static double DistanceTo(this Coordinates baseCoordinates, Coordinates targetCoordinates)
                    {
                        return DistanceTo(baseCoordinates, targetCoordinates, UnitOfLength.Kilometers);
                    }
                
                    public static double DistanceTo(this Coordinates baseCoordinates, Coordinates targetCoordinates, UnitOfLength unitOfLength)
                    {
                        var baseRad = Math.PI * baseCoordinates.Latitude / 180;
                        var targetRad = Math.PI * targetCoordinates.Latitude/ 180;
                        var theta = baseCoordinates.Longitude - targetCoordinates.Longitude;
                        var thetaRad = Math.PI * theta / 180;
                
                        double dist =
                            Math.Sin(baseRad) * Math.Sin(targetRad) + Math.Cos(baseRad) *
                            Math.Cos(targetRad) * Math.Cos(thetaRad);
                        dist = Math.Acos(dist);
                
                        dist = dist * 180 / Math.PI;
                        dist = dist * 60 * 1.1515;
                
                        return unitOfLength.ConvertFromMiles(dist);
                    }
                }
                
                public class UnitOfLength
                {
                    public static UnitOfLength Kilometers = new UnitOfLength(1.609344);
                    public static UnitOfLength NauticalMiles = new UnitOfLength(0.8684);
                    public static UnitOfLength Miles = new UnitOfLength(1);
                
                    private readonly double _fromMilesFactor;
                
                    private UnitOfLength(double fromMilesFactor)
                    {
                        _fromMilesFactor = fromMilesFactor;
                    }
                
                    public double ConvertFromMiles(double input)
                    {
                        return input*_fromMilesFactor;
                    }
                } 
                

              【讨论】:

              • 你能提供用于这个微积分的公式或者一些关于什么线的cmets吗?我必须更改什么才能直接以公里而不是英里为单位获得结果距离而无需转换?
              • 感谢您提供好的解决方案,我现在可以在我的桌面应用程序中使用它。
              • 在我无法使用 GeoCoordinate 的 UWP 应用中表现出色。
              • 计算结果为 95%。下面的函数是 100% 准确的:*.com/a/51839058/3736063
              【解决方案10】:

              GeoCoordinate 类(.NET Framework 4 及更高版本)已经有 GetDistanceTo 方法。

              var sCoord = new GeoCoordinate(sLatitude, sLongitude);
              var eCoord = new GeoCoordinate(eLatitude, eLongitude);
              
              return sCoord.GetDistanceTo(eCoord);
              

              距离以米为单位。

              您需要引用 System.Device。

              【讨论】:

              • Nigel,您确定 DistanceTo 方法可以在手机上使用吗?我以为它使用了 WP7 的 2.0 版 GeoCoordinate。
              • 我确实检查了这一点,并且设备的地理坐标有一个 GetDistanceTo 方法,这是您引用的方法(但不是您上面引用的方法)。没什么大不了。我将对此进行测试,看看内置的计算是否更好。谢谢奈杰尔!
              • 我可能会问一个错误的问题,但结果的单位是什么?是英里还是公里。我在任何地方都找不到它。
              • @SaeedNamati - 根据msdn.microsoft.com/en-us/library/… 的说法,它也在寻找这个 - 它以米为单位。
              • 是的,GeoCoordinate.GetDistanceTo() 返回以米为单位的值。对我来说,在美国,如果小于 1610,我将其转换为英尺(米 * 3.28084),否则我转换为英里(米 * 0.000621371)。对于我的目的来说,准确性已经绰绰有余了。
              【解决方案11】:

              正在计算经纬度点之间的距离...

                      double Lat1 = Convert.ToDouble(latitude);
                      double Long1 = Convert.ToDouble(longitude);
              
                      double Lat2 = 30.678;
                      double Long2 = 45.786;
                      double circumference = 40000.0; // Earth's circumference at the equator in km
                      double distance = 0.0;
                      double latitude1Rad = DegreesToRadians(Lat1);
                      double latititude2Rad = DegreesToRadians(Lat2);
                      double longitude1Rad = DegreesToRadians(Long1);
                      double longitude2Rad = DegreesToRadians(Long2);
                      double logitudeDiff = Math.Abs(longitude1Rad - longitude2Rad);
                      if (logitudeDiff > Math.PI)
                      {
                          logitudeDiff = 2.0 * Math.PI - logitudeDiff;
                      }
                      double angleCalculation =
                          Math.Acos(
                            Math.Sin(latititude2Rad) * Math.Sin(latitude1Rad) +
                            Math.Cos(latititude2Rad) * Math.Cos(latitude1Rad) * Math.Cos(logitudeDiff));
                      distance = circumference * angleCalculation / (2.0 * Math.PI);
                      return distance;
              

              【讨论】:

                【解决方案12】:

                试试这个:

                    public double getDistance(GeoCoordinate p1, GeoCoordinate p2)
                    {
                        double d = p1.Latitude * 0.017453292519943295;
                        double num3 = p1.Longitude * 0.017453292519943295;
                        double num4 = p2.Latitude * 0.017453292519943295;
                        double num5 = p2.Longitude * 0.017453292519943295;
                        double num6 = num5 - num3;
                        double num7 = num4 - d;
                        double num8 = Math.Pow(Math.Sin(num7 / 2.0), 2.0) + ((Math.Cos(d) * Math.Cos(num4)) * Math.Pow(Math.Sin(num6 / 2.0), 2.0));
                        double num9 = 2.0 * Math.Atan2(Math.Sqrt(num8), Math.Sqrt(1.0 - num8));
                        return (6376500.0 * num9);
                    }
                

                【讨论】:

                  【解决方案13】:

                  这里是 JavaScript 版本的男生和女生

                  function distanceTo(lat1, lon1, lat2, lon2, unit) {
                        var rlat1 = Math.PI * lat1/180
                        var rlat2 = Math.PI * lat2/180
                        var rlon1 = Math.PI * lon1/180
                        var rlon2 = Math.PI * lon2/180
                        var theta = lon1-lon2
                        var rtheta = Math.PI * theta/180
                        var dist = Math.sin(rlat1) * Math.sin(rlat2) + Math.cos(rlat1) * Math.cos(rlat2) * Math.cos(rtheta);
                        dist = Math.acos(dist)
                        dist = dist * 180/Math.PI
                        dist = dist * 60 * 1.1515
                        if (unit=="K") { dist = dist * 1.609344 }
                        if (unit=="N") { dist = dist * 0.8684 }
                        return dist
                  }
                  

                  【讨论】:

                  • 你需要 rlon1 和 `rlon2´ 做什么?