Google map 两点距离算法 求助

2025-02-23 23:17:34
推荐回答(1个)
回答1:

前端的话, google-maip api里有提供相应的方法: 

var LatLng1 = new google.maps.LatLng(23.2716270539,113.6719335938);
var LatLng2 = new google.maps.LatLng(30.0310554265,119.4471386719);
var distanceMeter = google.maps.geometry.spherical.computeDistanceBetween(LatLng1, LatLng2)


后端的话, java的栗子:

/**
 * 2016/08/19 经纬度距离换算, 北纬东经为正数
 * @author sleest
 */
public class Answer {
    private static final double EARTH_RADIUS = 6378137;

    public static double GetDistance(double longitude1, double latitude1, double longitude2,
            double latitude2) {
        double radLat1 = rad(latitude1);
        double radLat2 = rad(latitude2);
        double a = radLat1 - radLat2;
        double b = rad(longitude1) - rad(longitude2);
        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;
        s = Math.round(s * 10000) / 10000;
        return s;
    }

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

    public static void main(String[] args) {
        System.out
                .println(GetDistance(113.6719335938, 23.2716270539, 119.4471386719, 30.0310554265));
    }
}


注: 这里的距离的单位都是米.