两个地点之间的距离不正确。

5

我使用了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.1812千米。
我的应用程序将结果(d)显示为0.230千米。
请检查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个回答

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.

3

你的实现是正确的。给定这些经纬度应该产生一个距离为0.230公里。然而,坐标的正常输入顺序是(纬度,经度)。将它们反过来输入(经度,纬度)会产生不正确的距离0.1812公里


1
哦...有点尴尬。谢谢你的帮助 :) - Ally

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));


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接