Get the distance between two locations in android? Get the distance between two locations in android? android android

Get the distance between two locations in android?


Use the Google Maps Directions API. You'll need to request the directions over HTTP. You can do this directly from Android, or via your own server.

For example, directions from Montreal to Toronto:

GET http://maps.googleapis.com/maps/api/directions/json?origin=Toronto&destination=Montreal&sensor=false

You'll end up with some JSON. In routes[].legs[].distance, you'll get an object like this:

     "legs" : [        {           "distance" : {              "text" : "542 km",              "value" : 542389           },

You can also get the polyline information directly from the response object.


As Chris Broadfoot is correct, to parse returned JSON routes[].legs[].distance

"legs" : [        {           "distance" : {              "text" : "542 km",              "value" : 542389           }

Use:

    final JSONObject json = new JSONObject(result);    JSONArray routeArray = json.getJSONArray("routes");    JSONObject routes = routeArray.getJSONObject(0);    JSONArray newTempARr = routes.getJSONArray("legs");    JSONObject newDisTimeOb = newTempARr.getJSONObject(0);    JSONObject distOb = newDisTimeOb.getJSONObject("distance");    JSONObject timeOb = newDisTimeOb.getJSONObject("duration");    Log.i("Diatance :", distOb.getString("text"));    Log.i("Time :", timeOb.getString("text"));


public String getDistance(final double lat1, final double lon1, final double lat2, final double lon2){    String parsedDistance;    String response;    Thread thread=new Thread(new Runnable() {      @Override      public void run() {        try {          URL url = new URL("http://maps.googleapis.com/maps/api/directions/json?origin=" + lat1 + "," + lon1 + "&destination=" + lat2 + "," + lon2 + "&sensor=false&units=metric&mode=driving");          final HttpURLConnection conn = (HttpURLConnection) url.openConnection();          conn.setRequestMethod("POST");          InputStream in = new BufferedInputStream(conn.getInputStream());          response = org.apache.commons.io.IOUtils.toString(in, "UTF-8");          JSONObject jsonObject = new JSONObject(response);          JSONArray array = jsonObject.getJSONArray("routes");          JSONObject routes = array.getJSONObject(0);          JSONArray legs = routes.getJSONArray("legs");          JSONObject steps = legs.getJSONObject(0);          JSONObject distance = steps.getJSONObject("distance");          parsedDistance=distance.getString("text");        } catch (ProtocolException e) {          e.printStackTrace();        } catch (MalformedURLException e) {          e.printStackTrace();        } catch (IOException e) {          e.printStackTrace();        } catch (JSONException e) {          e.printStackTrace();        }      }    });    thread.start();    try {      thread.join();    } catch (InterruptedException e) {      e.printStackTrace();    }    return parsedDistance;}