2011-05-09 86 views
10
double distance; 

Location locationA = new Location("point A"); 

locationA.setLatitude(latA); 
locationA.setLongitude(lngA); 

Location locationB = new Location("point B"); 

locationB.setLatitude(latB); 
LocationB.setLongitude(lngB); 

distance = locationA.distanceTo(locationB); 

上面的代碼不起作用,我距離0.0 Km? 同樣在位置類的構造函數中,字符串提供者是什麼意思。 在上面的代碼中,我使用PointA和PointB作爲提供者。如何找到兩個地點之間的距離?

爲什麼上面的代碼無法正常工作?

謝謝你提前。

logcat的 05-09 17:48:56.144:INFO /電流LOC(1573):LAT 0.0 LNG 0.0 05-09 17:48:56.155:INFO/checklocation LOC(1573):LAT 54.4288665 LNG 10.169366

+1

你確定你的latA/latB和lngA/lngB的值是不同的,並且在正確的範圍內?你能提供你正在使用的值的例子嗎?此外,請注意'distanceTo'的結果是以米爲單位,而不是千米。 – Dave 2011-05-09 12:48:54

+0

是的。我正在轉換爲米。我已經把logcat的結果。我得到的值是不同的位置...... – user590849 2011-05-09 13:01:52

+0

你不需要轉換爲米,結果已經在米。請發佈完整的代碼示例,因爲我相信這裏的問題不在您發佈的代碼中。 – Dave 2011-05-10 12:27:35

回答

14

嘗試Location.distanceBetween(..)

更新:

如果你正在LAT從GeoPoint對象/ LON那麼他們在微度。你必須乘以1e6。

+0

對於相同的輸入,這會得到相同的結果。我相信在使用的輸入中存在一個問題,從原始問題中未顯示的代碼。 – Dave 2011-05-10 12:35:43

+0

@ user590849 - 你可能從GeoPoint獲得lon/lat嗎?那些是微觀的 - 你應該乘以1e6。 – 2011-05-10 12:38:43

1

實現上述的另一種方式,

public class Distance { 

    public static double distance(double lat1, double lon1, double lat2, double lon2) { 

     double theta = lon1 - lon2; 

     double dist = Math.sin(deg2rad(lat1)) * Math.sin(deg2rad(lat2)) 
       + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) 
       * Math.cos(deg2rad(theta)); 
     dist = Math.acos(dist); 
     dist = rad2deg(dist); 
     dist = dist * 60 * 1.1515; 
     //if (unit == "K") { 
     // dist = dist * 1.609344; 
     // else if (unit == "N") { 
     //dist = dist * 0.8684; 
     //} 
     return (dist); 
    } 


    public static final double PI = 3.14159265; 
    public static final double deg2radians = PI/180.0; 


    public static double getDistance(double latitude1, double longitude1, double latitude2,double longitude2) { 

     double lat1 = latitude1 * deg2radians; 
     double lat2 = latitude2 * deg2radians; 
     double lon1 = longitude1 * deg2radians; 
     double lon2 = longitude2 * deg2radians; 
     // Williams gives two formulae; 
     // this is the more accurate for close distances. 
     // In practice, the two differed only in the 8th or 9th place, for 
     // separations as small as 1 degree. 
     double radd = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin((lat1 - lat2)/2), 
       2.0) 
       + Math.cos(lat1) 
       * Math.cos(lat2) 
       * Math.pow(Math.sin((lon1 - lon2)/2), 2.0))); 

     return radd; 
    } 





    /* ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: */ 
    /* :: This function converts decimal degrees to radians : */ 
    /* ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: */ 
    private static double deg2rad(double deg) { 
     return (deg * Math.PI/180.0); 
    } 

    /* ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: */ 
    /* :: This function converts radians to decimal degrees : */ 
    /* ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: */ 
    private static double rad2deg(double rad) { 
     return (rad * 180.0/Math.PI); 
    } 

} 
0
double CalculateDistance(double nLat1, double nLon1, double nLat2, double nLon2) 
{ 
    double nRadius = 6371; // Earth's radius in Kilometers 
    // Get the difference between our two points 
    // then convert the difference into radians 

    double nDLat = ToRad(nLat2 - nLat1); 
    double nDLon = ToRad(nLon2 - nLon1); 

    // Here is the new line 
    nLat1 = ToRad(nLat1); 
    nLat2 = ToRad(nLat2); 

    double nA = pow (sin(nDLat/2), 2) + cos(nLat1) * cos(nLat2) * pow (sin(nDLon/2), 2); 

    double nC = 2 * atan2(sqrt(nA), sqrt(1 - nA)); 
    double nD = nRadius * nC; 

    return nD; // Return our calculated distance 
} 

http://www.jaimerios.com/?p=39

+0

發佈「distanceBetween(a,b)」這樣的代碼片段並不是一個答案。這裏的問題是爲什麼'locationA.distanceTo(locationB)'(據稱)給出了0米的結果? – Dave 2011-05-10 12:26:00

2

這不是答案,但請注意Location構造函數的簽名是Location(String provider)

即你傳遞給構造函數字符串應該是LocationManager.GPS_PROVIDERLocationManager.NETWORK_PROVIDERLocationManager.PASSIVE_PROVIDER之一,"point A""point B"

16

只是一個快速的片段,因爲我沒有看到上面GeoPoints一個完整的,簡單的解決方案:

public float getDistanceInMiles(GeoPoint p1, GeoPoint p2) { 
    double lat1 = ((double)p1.getLatitudeE6())/1e6; 
    double lng1 = ((double)p1.getLongitudeE6())/1e6; 
    double lat2 = ((double)p2.getLatitudeE6())/1e6; 
    double lng2 = ((double)p2.getLongitudeE6())/1e6; 
    float [] dist = new float[1]; 
    Location.distanceBetween(lat1, lng1, lat2, lng2, dist); 
    return dist[0] * 0.000621371192f; 
} 

如果你想米,剛剛返回DIST [0]直接。

+0

完美的作品,謝謝! – jbc25 2013-01-29 21:20:37

9

如上所述,位置類是要走的路。這裏是我用過的代碼:

Location locationA = new Location("point A"); 

locationA.setLatitude(pointA.getLatitudeE6()/1E6); 
locationA.setLongitude(pointA.getLongitudeE6()/1E6); 

Location locationB = new Location("point B"); 

locationB.setLatitude(pointB.getLatitudeE6()/1E6); 
locationB.setLongitude(pointB.getLongitudeE6()/1E6); 

double distance = locationA.distanceTo(locationB); 

在這個例子中,pointA和pointB都是GeoPoint類的實例。

相關問題