我有两个位置坐标(纬度和经度)。我想找到它们之间的距离,但是输出错误。
这是代码
public static String distanceBetweenTwoPoint(double lng1, double lat1, double lng2, double lat2) {
Location loc1 = new Location("Point A");
loc1.setLatitude(lat1);
loc1.setLongitude(lng1);
Location loc2 = new Location("Point B");
loc2.setLongitude(lng2);
loc2.setLatitude(lat2);
float distanceInMeter = loc1.distanceTo(loc2);
float distanceInKM = distanceInMeter / 1000;
Log.v(TAG, "Distance in Meter: " + distanceInMeter + " | Distance in KM: " + distanceInKM);
return String.valueOf(distanceInKM);
}
正确的输出应该等于 1.806公里
错误的输出= 4771.8936 KM
答案 0 :(得分:0)
What values are you passing to that function? And maybe the problem is the order. Perhaps loc2.distanceTo(loc1)
gives you the distance the other way around, which seems to be the problem.