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

知道兩個點的經緯度計算兩個點的距離

經緯度首先轉化為弧度

然後使用公式即可

 1     private     static double EARTH_RADIUS=6378.137;
 2     private static double rad(double d) {
 3             return d*Math.PI/180.0;
 4     }
 5     public static double getDistance(double latitude1,double longitude1,double latitude2, double longitude2 ) {
 6         double
distance=0; 7 double radLatitude1=rad(latitude1); 8 double radLatitude2=rad(latitude2); 9 double dis1=radLatitude1-radLatitude2; 10 double dis2=rad(longitude1)-rad(longitude2); 11 distance=2*Math.asin(Math.sqrt(Math.pow(Math.sin(dis1/2), 2)+Math.cos(radLatitude1)*Math.cos(radLatitude2)*Math.pow(Math.sin(dis2/2), 2)));
12 distance=distance*EARTH_RADIUS; 13 distance=Math.round(distance*10000d)/10000d; 14 distance=distance*1000; 15 return distance; 16 } 17 public static double getDistance2(double x1,double y1,double x2,double y2 ) { 18 double distance=0; 19 distance=Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
20 return distance; 21 } 22