[我尝试在android studio中计算Google地图中两点之间的距离,并且值与原始Google地图不同。结果差异很大。
从港口到马德里,结果范围超过100公里...
这是我的代码:
double lat1 = toRadians(latposto);
double lat2 = toRadians(myLocationLat);
double long1 = toRadians(longposto);
double long2 = toRadians(myLocationLong);
double dist = acos(sin(lat1) * sin(lat2) + cos(lat1) * cos(lat2) * cos(long1 - long2));
if(dist < 0) {
dist = dist + Math.PI;
}
double distancia = Math.round(dist * 6378100) * 0.001;
String distanciakm = String.format("%.2f", distancia);
km.setText(String.valueOf(distanciakm) + " km");
I have tried this way too and the result is the same ...:
double distance;
Location locationA = new Location("");
locationA.setLatitude(address.getLatitude());
locationA.setLongitude(address.getLongitude());
Location locationB = new Location("");
locationB.setLatitude(myLocationLat);
locationB.setLongitude(myLocationLong);
distance = locationA.distanceTo(locationB)/1000;
// kmeter.setText(String.valueOf(distance));
如果距离较长,结果可能会有所不同,您使用的是球形地球模型,我猜想distanceTo
方法是基于像Vincenty公式这样的更精确的值。