两个地理位置之间的距离

Sun*_*nny 2 android distance latitude-longitude

以下两行计算lat和lng

double geoLat = location.getLatitude(); //  37.422006
double geoLng = location.getLongitude(); //   -122.084095
Run Code Online (Sandbox Code Playgroud)

在循环中,我计算地理点之间的距离数组.latitude [i]和经度[i]来自google map api的json响应

distance[i]=GetLatAndLng.gps2m(geoLat, geoLng,latitude[i] ,longitude[i]);
Run Code Online (Sandbox Code Playgroud)

这是gps2m方法.

public static double gps2m(double lat1, double lng1, double lat2, double lng2) {
 double l1 = Math.toRadians(lat1);
    double l2 = Math.toRadians(lat2);
    double g1 = Math.toRadians(lng1);
    double g2 = Math.toRadians(lng2);

    double dist = Math.acos(Math.sin(l1) * Math.sin(l2) + Math.cos(l1) * Math.cos(l2) * Math.cos(g1 - g2));
    if(dist < 0) {
        dist = dist + Math.PI;
    }
    return Math.round(dist * 6378100);//in meters

}
}
Run Code Online (Sandbox Code Playgroud)

在距离数组我得到像1.1967617E7的值我想要以米为单位的距离

出了什么问题以及如何以米为单位的距离.

救命!!

Fra*_*nke 5

您可以使用Location类.您可以通过两种方式计算距离:

location.distanceTo(anotherLocation);
Run Code Online (Sandbox Code Playgroud)

要么

Location.distanceBetween(double startLatitude, double startLongitude, double endLatitude, double endLongitude, float[] results);
Run Code Online (Sandbox Code Playgroud)

有关详细信息,请查看位置文档.