我有2个文本框,我在其中提供源和目标地址。
1)来源= SHAHIBAUG UNDER BRIDGE,Shahibagh,Ahmedabad,古吉拉特邦
2)目的地= CG Road,Shreyas Colony,Navrangpura,Ahmedabad,Gujarat
距离= 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个参数只获得一个结果。这会有什么不同吗?任何人都有任何想法请帮助。
答案 0 :(得分:1)
谷歌地图为您提供道路距离,地理编码器为您提供了乌鸦飞行的距离。
答案 1 :(得分:0)
使用Google Maps API解决了这个问题。希望它能帮助其他人。它为您提供准确的距离。
final ProgressDialog dialog = ProgressDialog.show(AutoActivity.this, "", "Calculating...");
new Thread(new Runnable() {
@Override
public void run() {
try {
float distance = 0.0f;
StringBuffer url = new StringBuffer("http://maps.googleapis.com/maps/api/directions/json?origin=");
url.append(source_input.getText().toString().replace(" ", "").trim().toString());
url.append("&destination=");
url.append(destination_input.getText().toString().replace(" ", "").trim().toString());
url.append("&sensor=false");
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url.toString());
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
String line = EntityUtils.toString(httpEntity);
JSONObject jsonObject = new JSONObject(line);
JSONArray jsonarray = jsonObject.getJSONArray("routes").getJSONObject(0).getJSONArray("legs");
for (int i = 0; i < jsonarray.length(); i++) {
JSONObject obj = jsonarray.getJSONObject(i);
distance = Float.valueOf(obj.getJSONObject("distance").getString("text").replace("km", ""));
System.out.println("Distance == " + obj.getJSONObject("distance").getString("text").replace("km", ""));
}
kmVal = BigDecimal.valueOf(distance);
calculateFares();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (dialog!=null) {
dialog.dismiss();
}
}
}
}).start();