码迷,mamicode.com
首页 > Web开发 > 详细

计算2个坐标(经纬度)之间的距离,.Net

时间:2014-12-10 18:00:43      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   ar   color   os   sp   on   div   

.Net 中计算2个点之间的距离的方法,或者叫计算2个坐标,2个经纬度之间的距离。

代码如下:

 1 /// <summary>
 2     /// Calculates the distance between two geographics locations
 3     /// </summary>
 4     public static class GeoDistance
 5     {
 6         public static double Between(double latitude1, double longitude1, double latitude2, double longitude2,
 7             DistanceUnits units = DistanceUnits.Kilometers)
 8         {
 9             double a1 = latitude1 * RadianConversionFactor;
10             double b1 = longitude1 * RadianConversionFactor;
11             double a2 = latitude2 * RadianConversionFactor;
12             double b2 = longitude2 * RadianConversionFactor;
13 
14             double distance =
15                 Math.Acos(Math.Cos(a1) * Math.Cos(b1) * Math.Cos(a2) * Math.Cos(b2) +
16                     Math.Cos(a1) * Math.Sin(b1) * Math.Cos(a2) * Math.Sin(b2) + Math.Sin(a1) * Math.Sin(a2)) * DistanceConstants[units];
17 
18             return distance;
19         }
20 
21         private const double RadianConversionFactor = Math.PI / 180;
22 
23         private static readonly Dictionary<DistanceUnits, double> DistanceConstants = new Dictionary<DistanceUnits, double>(3) {
24             { DistanceUnits.Miles, 3963.1 },
25             { DistanceUnits.NauticalMiles, 3443.9 },
26             { DistanceUnits.Kilometers, 6378 }
27         };
28     }
29 
30     public enum DistanceUnits
31     {
32         Miles,
33         NauticalMiles,
34         Kilometers,
35     }

代码很简单,但是费了不少劲。就是还没测试效率怎么样。如果有高效的算法,请不要吝啬指教!!!

计算2个坐标(经纬度)之间的距离,.Net

标签:style   blog   io   ar   color   os   sp   on   div   

原文地址:http://www.cnblogs.com/rockyyue/p/4155954.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!