2013-10-14 201 views
0

我試圖創建一個函數,以便根據我在哪裏和其他點返回地圖上各個點之間的距離。點之間的距離

但是這樣可以得到在數據庫第一點獲得的距離。

你能幫我嗎?

if (cursor.getCount() > 0) { 
    if (cursor != null && cursor.moveToFirst()) {  
     lat_s = >cursor.getString(cursor.getColumnIndex("latitude")); 
     lng_s = >cursor.getString(cursor.getColumnIndex("longitude"));   
     lat_p = Double.parseDouble(lat_s); 
     lng_p = Double.parseDouble(lng_s);   
     double dist = getDistancia(lat, lng, lat_p, lng_p);   
    }  
    @SuppressWarnings("deprecation")   
    SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, 
     R.layout.list_row, cursor, from, to);  
    listView.setAdapter(mAdapter); 
} 

,並返回我的距離的函數是:

public double getDistancia(double latitude, double longitude, 
     double latitudePto, double longitudePto){ 
    double dlon, dlat, a, distancia; 
    dlon = longitudePto - longitude; 
    dlat = latitudePto - latitude; 
    a = Math.pow(Math.sin(dlat/2),2) + Math.cos(latitude) * 
      Math.cos(latitudePto) * Math.pow(Math.sin(dlon/2),2); 
    distancia = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
    return 6378140 * distancia; /* 6378140 is the radius of the Earth in meters*/ 
} 
+0

我有在SO上已經回答了類似的問題。請檢查此鏈接:http://stackoverflow.com/questions/19332850/calculate-shortest-path-between-two-geo-points/19355447#19355447 – Arshu

+0

看看[這] [1]類似的問題。這有很多有用的答案。 [1]:http://stackoverflow.com/questions/19218081/how-to-calculate-distance-from-different-markers-in-a-map-and-then-pick-up- -l – Akshat

回答

0

不明白的問題,但這裏是解決方案的元素:

protected double distanceBetween(float lat_a, float lng_a, float lat_b, float lng_b) { 

    float pk = (float) (180/3.14169); 
    float a1 = lat_a/pk; 
    float a2 = lng_a/pk; 
    float b1 = lat_b/pk; 
    float b2 = lng_b/pk; 
    float t1 = FloatMath.cos(a1) * FloatMath.cos(a2) * FloatMath.cos(b1) * FloatMath.cos(b2); 
    float t2 = FloatMath.cos(a1) * FloatMath.sin(a2) * FloatMath.cos(b1) * FloatMath.sin(b2); 
    float t3 = FloatMath.sin(a1) * FloatMath.sin(b1); 
    double tt = Math.acos(t1 + t2 + t3); 
    return 6366000 * tt; 
} 
1

谷歌地圖提供的API獲取兩個位置之間的距離 -

更多詳情

https://developers.google.com/maps/documentation/directions/

您也可以使用這個 -

Location locationA = new Location("point A"); 
locationA.setLatitude(mLatitude/1E6); 
locationA.setLongitude(mLongitude/1E6); 
Location locationB = new Location("point B"); 
locationB.setLatitude(lat/1E6); 
locationB.setLongitude(lng/1E6); 
double distance = locationA.distanceTo(locationB); 
+0

我的問題是我怎樣才能提出幾點,通過「光標」提醒,因爲它已經試圖獲得一個數組,但不能,兩點之間的距離已經是,但我是其中之一。 –

+0

你可以將你的位置經緯度存儲在arraylist中,然後將它們循環傳遞給google api(https://developers.google.com/maps/documentation/directions/)。使用json解析你可以逐個獲取。 –