获取两点(经纬度表示)间距离

时间:2021-06-21 19:52:48
 

获取两点(经纬度表示)间距离

/3echo 2010-4-28

经常有朋友或同事征询有关如何获取两点(经纬度表示)间距离的问题,现将解决方法写出来,以供自己和朋友查询、学习。

解决此问题的关键是要理解空间几何模型,在理解空间几何模型的基础上再利用数学公式求取两点之间的值。其解决步骤如下:

1、设两点分别为P1P2,如果其值是用度分秒形式表示,则需将其转换成十进制度的形式,如P1点纬度为2330分,则其纬度值转换成十进制度的形式为23.5度。如果值为十进制度的形式,则直接进入第二步。

2、分别将两点的经度、纬度值转换成弧度制形式,如P1纬度为23.5度,转换成弧度制则为:23.5*PI / 180。分别用 P1latInRadP1LongInRadP2latInRadP2LongInRad表示。

3、分别求取两点间的纬度差(dlat)与经度差(dlon);

4、求取两点间的正弦与余弦值,公式如下:

A=sin2(dlat/2) + cos(P1LatInRad)*cos(P2LatInRad)*Sin2(dlon/2)       (1)

5、求取两点的正切值,公式如下:

C=2*Math.Atan2(Math.Sqrt(A), Math.Sqrt(1-A))                     (2)

6、返回两点间的距离:公式如下:

D=EarthRadiusKm * C                                          (3)

 

C#源代码:

 

获取两点(经纬度表示)间距离获取两点(经纬度表示)间距离获取两点间距离
 1         ///   <summary>
 2         ///  获取两点(经纬度表示)间的距离
 3         ///   </summary>
 4         ///   <param name="p1Lat"> 第一点纬度值 </param>
 5         ///   <param name="p1Lng"> 第一点经度值 </param>
 6         ///   <param name="p2Lat"> 第二点纬度值 </param>
 7         ///   <param name="p2Lng"> 第二点经度值 </param>
 8         ///   <returns> 返回两点间距离 </returns>
 9         public   double  GetDistance( double  p1Lat,  double  p1Lng,  double  p2Lat,  double  p2Lng)
10        {
11            double  dLat1InRad  =  p1Lat  *  (Math.PI  /   180 );
12            double  dLong1InRad  =  p1Lng  *  (Math.PI  /   180 );
13            double  dLat2InRad  =  p2Lat  *  (Math.PI  /   180 );
14            double  dLong2InRad  =  p2Lng  *  (Math.PI  /   180 );
15            double  dLongitude  =  dLong2InRad  -  dLong1InRad;
16            double  dLatitude  =  dLat2InRad  -  dLat1InRad;
17            double  a  =  Math.Pow(Math.Sin(dLatitude  /   2 ),  2 +  Math.Cos(dLat1InRad)  *  Math.Cos(dLat2InRad)  *  Math.Pow(Math.Sin(dLongitude  /   2 ),  2 );
18            double  c  =   2   *  Math.Atan2(Math.Sqrt(a), Math.Sqrt( 1   -  a));
19            double  dDistance  =  EarthRadiusKm  *  c;
20            return  dDistance;
21        }
22 
23         ///   <summary>
24         ///  Radius of the Earth
25         ///   </summary>
26         public   double  EarthRadiusKm  =   6378.137 //  WGS-84