下面两行计算纬度和经度
double geoLat = location.getLatitude(); // 37.422006
double geoLng = location.getLongitude(); // -122.084095
在一个循环中,我计算地理点之间的距离数组。 latitude[i] 和 longitude[i] 来自 google map api 的 json 响应
distance[i]=GetLatAndLng.gps2m(geoLat, geoLng,latitude[i] ,longitude[i]);
这是 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
}
}
在距离数组中,我得到的值类似于 1.1967617E7 我想要以米为单位的距离
出了什么问题以及如何获得以米为单位的距离。
求助!!
最佳答案
您可以使用 Location 类。您可以通过两种方式计算距离:
location.distanceTo(anotherLocation);
或
Location.distanceBetween(double startLatitude, double startLongitude, double endLatitude, double endLongitude, float[] results);
看看 Location documentation了解更多详情。
关于android - 两个地理点之间的距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8105059/