1. 程式人生 > >計算兩個經緯度點的實際距離

計算兩個經緯度點的實際距離

一、概述

因為地球是個球形,所以地球上的兩個點,實際上是球面上的兩個點,要計算這兩個點之間的距離,不能簡單的通過直角座標系來計算。

 

二、計算方法

1、地球半徑取近似值 6378.137km

1 /**
2      * 地球半徑
3      * 6378.137km
4      */
5     public static final double EARTH_RADIUS = 6.371229*1e6;

2、實現程式碼

 1 /**
 2      * 求地球兩點距離
 3      * @param sLat
 4      * @param sLng
5 * @param eLat 6 * @param eLng 7 * @return 8 */ 9 public static double latitudeLongitudeDistEarth(double sLat, double sLng, double eLat, double eLng) 10 { 11 double x,y,out; 12 double PI=Math.PI; 13 14 x=(eLat-sLat)* PI * ExecutorConstant.EARTH_RADIUS * Math.cos( ((sLng+eLng)/2) * PI /180)/180;
15 y=(eLng-sLng)* PI * ExecutorConstant.EARTH_RADIUS /180; 16 out=Math.hypot(x,y); 17 return out; 18 }

以上只給出了公式,並沒有詳細的原理,僅供參考

參考資料:

http://mathforum.org/library/drmath/view/51879.html

http://blog.charlee.li/location-search/

http://www.cnblogs.com/softfair/p/distance_of_two_latitude_and_longitude_points.html