两个地点之间的距离不对

时间:2010-07-15 16:31:41

标签: android maps distance haversine

我在http://www.movable-type.co.uk/scripts/latlong.html上使用了算法来找到两点之间的距离。

我的两点是

long1 = 51.507467;
lat1 = -0.08776;

long2 = 51.508736;
lat2 = -0.08612;

根据Movable Type Script,答案是0.1812km

我的应用程序将结果(d)设为0.230km

检查Haversine公式:http://www.movable-type.co.uk/scripts/latlong.html

    double R = 6371; // earth’s radius (mean radius = 6,371km)
    double dLat =  Math.toRadians(lat2-lat1);

    double dLon =  Math.toRadians(long2-long1); 
    a = Math.sin(dLat/2) * Math.sin(dLat/2) +
            Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) * 
            Math.sin(dLon/2) * Math.sin(dLon/2); 
    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
    double d = R * c;

3 个答案:

答案 0 :(得分:9)

为什么重新发明自己的距离计算器,Location类内置了一个。

结帐

distanceBetween(double startLatitude, double startLongitude, double endLatitude, double endLongitude, float[] results) 
Computes the approximate distance in meters between two locations, and optionally the initial and final bearings of the shortest path between them.

答案 1 :(得分:3)

您的实施是正确的。给定经度和纬度的距​​离应该产生0.230 km的距离。然而,坐标的正常输入是(纬度,经度)。将它们放在后面(经度,纬度)会产生0.1812 km的错误距离。

答案 2 :(得分:1)

public double CalculationByDistance(GeoPoint StartP, GeoPoint EndP) {  
  double lat1 = StartP.getLatitudeE6()/1E6;  
  double lat2 = EndP.getLatitudeE6()/1E6;  
  double lon1 = StartP.getLongitudeE6()/1E6;  
  double lon2 = EndP.getLongitudeE6()/1E6;  
  double dLat = Math.toRadians(lat2-lat1);  
  double dLon = Math.toRadians(lon2-lon1);  
  double a = Math.sin(dLat/2) * Math.sin(dLat/2) +  
     Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) *  
     Math.sin(dLon/2) * Math.sin(dLon/2);  
  double c = 2 * Math.asin(Math.sqrt(a));  
  return Radius * c;  
 }  

Ally你的概念是对的。可能会在这一行中有点变化double c = 2 * Math.asin(Math.sqrt(a));