根据两点经纬度坐标计算距离的算法

来源:互联网 发布:mac卸载itools 编辑:程序博客网 时间:2024/05/16 12:32
  1. /**   
  2.  * Created by yuliang on 2015/3/20.   
  3.  */    
  4. public class LocationUtils {    
  5.     private static double EARTH_RADIUS = 6378.137;    
  6.     
  7.     private static double rad(double d) {    
  8.         return d * Math.PI / 180.0;    
  9.     }    
  10.     
  11.     /**   
  12.      * 通过经纬度获取距离(单位:米)   
  13.      * @param lat1   
  14.      * @param lng1   
  15.      * @param lat2   
  16.      * @param lng2   
  17.      * @return   
  18.      */    
  19.     public static double getDistance(double lat1, double lng1, double lat2,    
  20.                                      double lng2) {    
  21.         double radLat1 = rad(lat1);    
  22.         double radLat2 = rad(lat2);    
  23.         double a = radLat1 - radLat2;    
  24.         double b = rad(lng1) - rad(lng2);    
  25.         double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2)    
  26.                 + Math.cos(radLat1) * Math.cos(radLat2)    
  27.                 * Math.pow(Math.sin(b / 2), 2)));    
  28.         s = s * EARTH_RADIUS;    
  29.         s = Math.round(s * 10000d) / 10000d;    
  30.         s = s*1000;    
  31.         return s;    
  32.     }    
  33. }
转自http://blog.csdn.net/woaixinxin123/article/details/45935439
原创粉丝点击