一、概述
因为地球是个球形,所以地球上的两个点,实际上是球面上的两个点,要计算这两个点之间的距离,不能简单的通过直角坐标系来计算。
二、计算方法
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
扫描二维码关注公众号,回复:
4390118 查看本文章