Calculate distance of two geo points in km c#

匿名 (未验证) 提交于 2019-12-03 01:33:01

问题:

I`d like calculate the distance of two geo points. the points are given in longitude and latitude.

the coordinates are:

point 1: 36.578581, -118.291994

point 2: 36.23998, -116.83171

here a website to compare the results:

http://www.movable-type.co.uk/scripts/latlong.html

here the code I used from this link: Calculate distance between two points in google maps V3

    const double PIx = Math.PI;     const double RADIO = 6378.16;      ///      /// Convert degrees to Radians     ///      /// Degrees     /// The equivalent in radians     public static double Radians(double x)     {         return x * PIx / 180;     }      ///      /// Calculate the distance between two places.     ///      ///      ///      ///      ///      ///      public static double DistanceBetweenPlaces(double lon1, double lat1, double lon2, double lat2)     {         double R = 6371; // km         double dLat = Radians(lat2 - lat1);         double dLon = Radians(lon2 - lon1);         lat1 = Radians(lat1);         lat2 = Radians(lat2);          double a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) + Math.Sin(dLon / 2) * Math.Sin(dLon / 2) * Math.Cos(lat1) * Math.Cos(lat2);         double c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));         double d = R * c;          return d;     }   Console.WriteLine(DistanceAlgorithm.DistanceBetweenPlaces(36.578581, -118.291994, 36.23998, -116.83171)); 

the issue is that I get two different results.

my result: 163,307 km

result of the website: 136 km

any suggestions???

torti

回答1:

Your formula is almost correct, but you have to swap parameters for longitude an latitude

Console.WriteLine(DistanceAlgorithm.DistanceBetweenPlaces(-118.291994, 36.578581, -116.83171, 36.23998)); // = 136 km 

I'm using simplified formula:

Testing:

(Distance at Equator): Longitudes 0, 100; Latitudes = 0,0; DistanceBetweenPlaces(0, 0, 100, 0) = 11119.5 km

(Distance at North Pole): Longitudes 0, 100; Latitudes = 90,90; DistanceBetweenPlaces(0, 90, 100, 90) = 0 km

Longitudes: -118.291994, -116.83171; Latitudes: 36.578581, 36.23998 = 135.6 km

Longitudes: 36.578581, 36.23998; Latitudes: -118.291994, -116.83171 = 163.2 km

Best regards

P.S. At web site you use for result comparison, for every point first text box is latitude, second - longitude



回答2:

As you are using the framework 4.0, I would suggest the GeoCoordinate class.

// using System.Device.Location;  GeoCoordinate c1 = new GeoCoordinate(36.578581, -118.291994); GeoCoordinate c2 = new GeoCoordinate(36.23998, -116.83171);  double distanceInKm = c1.GetDistanceTo(c2) / 1000; // Your result is: 136,111419742602 

You have to add a reference to System.Device.dll.



回答3:

In my article published several years ago (link: http://www.codeproject.com/Articles/469500/Edumatter-School-Math-Calculators-and-Equation-Sol) I have described 3 useful Functions to calculate the distance between 2 geo-points (in other words, great-circle (orthodromic) distance on Earth betw

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!