首页 > 代码库 > 根据地球上两个坐标点,计算出距离

根据地球上两个坐标点,计算出距离

public class MapDistance {


 private final static double PI = 3.14159265358979323; // 圆周率
 private final static double R = 6371229; // 地球的半径

 public static double getDistance(double longt1, double lat1, double longt2,
   double lat2) {
  double x, y, distance;
  x = (longt2 - longt1) * PI * R
    * Math.cos(((lat1 + lat2) / 2) * PI / 180) / 180;
  y = (lat2 - lat1) * PI * R / 180;
  //System.out.println((Math.hypot(x, y) / 1000));
  //distance = Double.parseDouble(String.format("%.0f", Math.hypot(x, y) / 1000));
  distance = (Math.hypot(x, y) / 1000);
  return distance;
 }

 public static void main(String[] args) {
 
  System.out.println(getDistance(40.137004,116.394765, 116.337795,39.977523));
 }
 
}

根据地球上两个坐标点,计算出距离