两个纬度和经度之间的中点

2022-09-01 07:09:16

我正在尝试将本 http://www.movable-type.co.uk/scripts/latlong.html 中给出的代码片段转换为java。但是我没有得到与网站相同的结果。这是我的代码,用于查找两点之间的中点,其中给出了它们的纬度和经度

midPoint(12.870672,77.658964,12.974831,77.60935);
    public static void midPoint(double lat1,double lon1,double lat2,double lon2)
    {
   double dLon = Math.toRadians(lon2-lon1);
        double Bx = Math.cos(lat2) * Math.cos(dLon);
        double By = Math.cos(lat2) * Math.sin(dLon);
        double lat3 = Math.atan2(Math.sin(lat1)+Math.sin(lat2),Math.sqrt( (Math.cos(lat1)+Bx)*(Math.cos(lat1)+Bx) + By*By) );
        double lon3 = lon1 + Math.atan2(By, Math.cos(lat1) + Bx);
        System.out.print(lat3 +" " + lon3 );
    }

我不确定dLon是否正确。所以请帮帮我弄清楚。附言:我需要找到中点的纬度和经度


答案 1

您需要转换为弧度。将其更改为以下内容:

public static void midPoint(double lat1,double lon1,double lat2,double lon2){

    double dLon = Math.toRadians(lon2 - lon1);

    //convert to radians
    lat1 = Math.toRadians(lat1);
    lat2 = Math.toRadians(lat2);
    lon1 = Math.toRadians(lon1);

    double Bx = Math.cos(lat2) * Math.cos(dLon);
    double By = Math.cos(lat2) * Math.sin(dLon);
    double lat3 = Math.atan2(Math.sin(lat1) + Math.sin(lat2), Math.sqrt((Math.cos(lat1) + Bx) * (Math.cos(lat1) + Bx) + By * By));
    double lon3 = lon1 + Math.atan2(By, Math.cos(lat1) + Bx);

    //print out in degrees
    System.out.println(Math.toDegrees(lat3) + " " + Math.toDegrees(lon3));
}

答案 2

使用Android Google Maps Utilities更容易:

LatLngBounds bounds = new LatLngBounds(start, dest);
bounds.getCenter();

更新:更好地使用构建器(为什么看到坏失败者答案):

LatLngBounds.builder().include(start).include(dest).build().getCenter();