2017-11-11 86 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");