经纬度-计算地图两点间的距离

public class getDistance {

    private static final double EARTH_RADIUS = 6378137;// 赤道半径

    private static double rad(double d) {
        return d * Math.PI / 180.0;
    }

    public static int GetDistance(String lonA, String latA, String lonB,
            String latB) {
        double lon1 = Double.parseDouble(lonA);
        double lat1 = Double.parseDouble(latA);
        double lon2 = Double.parseDouble(lonB);
        double lat2 = Double.parseDouble(latB);
        double radLat1 = rad(lat1);
        double radLat2 = rad(lat2);
        double a = radLat1 - radLat2;
        double b = rad(lon1) - rad(lon2);
        double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2)
                + Math.cos(radLat1) * Math.cos(radLat2)
                * Math.pow(Math.sin(b / 2), 2)));
        s = s * EARTH_RADIUS;
        return (int) s;// 单位米
    }

    public static void main(String[] args) {
        System.out.println(GetDistance("0.000", "0.000", "1.000", "1.000"));
    }

}

你可能感兴趣的:(经纬度-计算地图两点间的距离)