我有2个文本框,我在其中提供源地址和目标地址。从Android中的地理编码器获得错误的距离
1)源= SHAHIBAUG桥下,Shahibagh,Ahmedabad,古吉拉特
2)目的地= CG路Shreyas菌落,Navrangpura,Ahmedabad,古吉拉特
距离= 4.6(从谷歌地图) & & 2.6656852(使用位置的distanceTo法)
final ProgressDialog dialog = ProgressDialog.show(AutoActivity.this, "Fare", "test");
new Thread(new Runnable() {
@Override
public void run() {
try {
Location source_location = new Location("");
Location destination_location = new Location("");
Geocoder geoCode = new Geocoder(getApplicationContext());
source_location.setLatitude(geoCode.getFromLocationName(source_input.getText().toString(), 1).get(0).getLatitude());
source_location.setLatitude(geoCode.getFromLocationName(source_input.getText().toString(), 1).get(0).getLongitude());
source_location.set(source_location);
destination_location.setLatitude(geoCode.getFromLocationName(destination_input.getText().toString(), 1).get(0).getLatitude());
destination_location.setLatitude(geoCode.getFromLocationName(destination_input.getText().toString(), 1).get(0).getLongitude());
destination_location.set(destination_location);
float distance = source_location.distanceTo(destination_location)/1000;
System.out.println("Distance == " + distance);
kmVal = BigDecimal.valueOf(distance);
calculateFares();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (dialog!=null) {
dialog.dismiss();
}
}
}
}).start();
我不知道为什么我错了距离。我在我的代码中有一个疑问,那就是我使用了getFromLocationName方法,并且在传递1个参数来获得一个结果。这有什么区别吗?任何人有任何想法,请帮助。
尝试计算来源lat,lng和目标lat的距离 –
我需要这2个位置的道路距离。是否可以通过Android中提供的地理编码器和位置API或必须使用Google地图。 – Scorpion
检查此问题http://stackoverflow.com/questions/7696112/gettting-different-distance-when-using-the-google-map-and-user-defined-function/7752341#7752341 – MKJParekh