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.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;
4

3 回答 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.
于 2010-07-15T17:41:23.867 回答
3

你的实现是正确的。给定这些经度和纬度的距​​离应产生0.230 km. 然而,坐标的正常输入是(纬度,经度)。将它们放在后面(经度,纬度)会产生不正确的距离0.1812 km

于 2010-07-15T17:25:18.330 回答
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;  
 }  

盟友你的概念是对的。这条线可能有点变化double c = 2 * Math.asin(Math.sqrt(a));

于 2012-05-15T11:16:43.047 回答