2013-02-14 85 views
0

我有一個問題得到正確的方式來從我的2地理點得到distanceTo。如何讓它工作?方法distanceTo(雙)是不明確的

從GPS類:

double latitude; // latitude 
String mlat; 

    latitude = location.getLatitude(); 
    mlat = String.valueOf(latitude); 

從ListView控件:

     // Get distance 
         String mReportA; 
         double mLocB; 
         int distance; 
         mReportA = e.getString("lat"); 
         mReportA = e.getString("lon"); 
         mLocB = gps.latitude; 
         mLocB = gps.longitude; 
         distance = mReportA.distanceTo(mLocB); 

錯誤在Eclipse:該方法distanceTo(double)是未定義String類型 我得到e.getString("lat");從JSON

+0

mReportA是字符串。 distanceTo是爲位置對象定義的。 – Anukool 2013-02-14 10:40:20

+0

yes,e.getString(「lat」);來自json。所以它是一個字符串。如何轉換它,它的工作? – sarahsdev 2013-02-14 10:43:06

+0

@sarahsdev看到我編輯的答案。 – rajeshwaran 2013-02-14 10:46:29

回答

1

要計算兩個GeoPoint對象之間的距離,你可以按照下面的例子。

double currentLatitude = location.getLatitude(); 
double currentLongitude = location.getLongitude(); 

double endLatitude = lat; 
double endLongitude = lng; 

float[] results = new float[3]; 
Location.distanceBetween(currentLatitude, currentLongitude,endLatitude, endLongitude, results); 

BigDecimal bd = new BigDecimal(results[0]);// results in meters 
BigDecimal rounded = bd.setScale(2, RoundingMode.HALF_UP); 
double values = rounded.doubleValue(); 

EDIT

if (values > 1000) { 
values = (Double) (values * 0.001f);// convert meters to Kilometers 
bd = new BigDecimal(values); 
rounded = bd.setScale(2, RoundingMode.HALF_UP); 
values = rounded.doubleValue(); 
}  
+0

我怎樣才能得到公里的結果? – sarahsdev 2013-02-18 14:46:06

+1

@sarahsdev看到我編輯的答案。 – KunalK 2013-02-18 16:42:54

1
// Here is the code to find out the distance between two locations 

float distance; 
Location locationA = new Location("A"); 

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

Location locationB = new Location("B"); 

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

distance = locationA.distanceTo(locationB); 
+0

我得到這個e.getString(「lat」);來自json。所以它是一個字符串。如何轉換它,它的工作? – sarahsdev 2013-02-14 10:41:43

+0

您可以獲取位置A和位置B的經度和緯度。現在你必須創建兩個Location對象。一個用於A和一個用於B。然後你可以使用distanceTo函數。 – Anukool 2013-02-14 10:45:44

3

您可以將緯度和經度值es是double然後只計算距離。所以沒有改變字符串的雙重值。

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

locationA.setLatitude(orginlat); 
locationA.setLongitude(orginlong); 

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

locationB.setLatitude(tolat); 
locationB.setLongitude(tolong); 

Float distance = locationA.distanceTo(locationB); 

字符串轉換爲

public static double parseDoubleSafely(String str) { 
    double result = 0; 
    try { 
     result = Double.parseDouble(str); 
    } catch (NullPointerException npe) { 
     //sysout found null 
     return 0; 
    } catch (NumberFormatException nfe) { 
     //sysout NFE 
     return 0; 
    } 
    return result; 
} 
+0

eclipse錯誤:無法在原始類型上調用distanceTo(double)double – sarahsdev 2013-02-14 11:08:55

+0

@sarahsdev distanceTo(location)只傳遞,不是double值。請參閱http://developer.android.com/reference/android/location/Location.html – rajeshwaran 2013-02-14 11:29:35

+0

@sarahsdev發佈您的代碼? – rajeshwaran 2013-02-14 11:38:36