2010-07-15 75 views
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; 

回答

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. 
+0

會試試:) – Ally 2010-07-15 18:41:04

+0

它工作!謝謝 – Ally 2010-07-15 18:51:36

3

您的實施是正確的。給定這些經度和緯度的距​​離應該產生0.230 km的距離。但是,座標的正常輸入是(緯度,經度)。將它們放在後面(經度,緯度)會產生不正確的距離0.1812 km

+1

哦......有些尷尬。感謝您的幫助:) – Ally 2010-07-15 18:53:19

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

盟友你的概念是right.May一點點昌在這一行double c = 2 * Math.asin(Math.sqrt(a));

相關問題