2017-11-11 254 views
-1

我正在嘗試計算我的地理位置點和跟蹤這兩者之間的折線的標記之間的距離,但問題是當我輸出結果並且它給了我一個滿足感時距離(1.125405E7)代替(例如)1,250米。使用DistanceTo時出現的問題(距離不正確)

這是我的代碼

//Gets the latitude & longitude of my Geolocation 

Location location = new Location("A"); 
       location.setLatitude(location.getLatitude()); 
       location.setLongitude(location.getLongitude()); 

//Gets the location of a marker/Polyline which i put using onMapLongCliclistener 

       Location locationb = new Location("B"); 
       locationb.setLatitude(latLng.latitude); 
       locationb.setLongitude(latLng.longitude); 

       //Calculates the distance between Geolocation/Marker 

       double distance=locationb.distanceTo(location); 

       String distancia = String.valueOf(distance); 
       Toast.makeText(MapsActivity.this,distancia, 
Toast.LENGTH_SHORT).show(); 

任何提示或解決方案?
非常感謝!

+0

要設置緯度'A'的經度兩次,從未設置的緯度。這是你的真實代碼還是錯誤? –

+0

@MarkusKauppinen固定,仍然給我很大的距離 – QuickAccount123

+0

你能提供兩個位置的實際座標和適當的距離結果嗎? –

回答

0

從你的代碼行號3'location.setLongitude(location.getLatitude());'是錯誤的。您正在設置經度而不是緯度。

+0

已經修復,檢查更新後的代碼。 – QuickAccount123

0

代碼:

double distance = SphericalUtil.computeDistanceBetween(new LatLng(location.getLatitude(),location.getLongitude()),new 
    LatLng(latLng.latitude,latLng.longitude)); 
    String distanceC = String.valueOf(distance); 
    txtDistancia.setText(new DecimalFormat("##.##").format(distanceC)+" "+"mts");