I am very new to android Google maps i write the code for find distance between two places:
private String getDistance(final String start, final String end) {
new Thread(new Runnable() {
@Override
public void run() {
float distance = 0.0f;
StringBuffer url = new StringBuffer("http://maps.googleapis.com/maps/api/directions/json?origin=");
url.append(start.replace(" ", "").trim().toString());
url.append("&destination=");
url.append(end.replace(" ", "").trim().toString());
url.append("&mode=transit");
url.append("&language=en-EN");
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", ""));
}
BigDecimal kmVal = BigDecimal.valueOf(distance);
System.out.println("===========distance=============="+kmVal);
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
return distance ;
}
I am getting the Distance with differences of 30 km. I am getting 30 km extra instead of actual distance.