2014-07-12 37 views
1

我得到使用此兩個位置:兩個地點之間的距離不對?

LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); 

然後我計算出它們之間的距離,但距離是不是真的

public static float calculateDistance(double e, double f, double g, double h) 
{ 
    float dLat = (float) Math.toRadians(g - e); 
    float dLon = (float) Math.toRadians(h - f); 
    float a = 
      (float) (Math.sin(dLat/2) * Math.sin(dLat/2) + Math.cos(Math.toRadians(e)) 
        * Math.cos(Math.toRadians(g)) * Math.sin(dLon/2) * Math.sin(dLon/2)); 
    float c = (float) (2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a))); 
    float d = 6371 * c; 
    return d; 
} 

(32.2163799,35.0420986) (31.9210915,35.2037014)

結果36.193707千米 但實際值超過85千米

+0

你怎麼知道diszance是不是真的?你從哪裏得到了85公里?您是否使用允許距離計算的網頁檢查距離? – AlexWien

回答

0

可以使用非標準的功能如下:

Location location1 = new Location("<provider name"); 
location1.setLatitude(lat1); 
location1.setLongitude(lng1); 

Location location2 = new Location("<provider name"); 
location2.setLatitude(lat2); 
location2.setLongitude(lng2); 

float distance = location1.distanceTo(location2); 

更多信息:http://developer.android.com/reference/android/location/Location.html#distanceTo%28android.location.Location%29

或功能之間的距離: http://developer.android.com/reference/android/location/Location.html#distanceBetween%28double,%20double,%20double,%20double,%20float[]%29

+0

Nickolai Astashonok仍然不對 –

0

使用靜態方法distanceBetween()得到米距離

float[] results = new float[3]; 
Location.distanceBetween(startLatitude, startLongitude, endLatitude, endLongitude, results); 
float distanceInMeters = results[0] 


請注意,結果陣列包在一起的幾個值與兩個點之間的距離:

- results[0] - distance in meters 
    - results[1] - initial bearing angle of the shortest path between them 
    - results[2] - final bearing angle of the shortest path between them 
+0

這給了我nullpointerException吉拉德Haimov –

+0

看到我的原始發佈更正 –