Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting distance from Google Direction API in Android

http://maps.googleapis.com/maps/api/directions/json?origin=-20.291825,57.448668&destination=-20.179724,57.613463&sensor=false&mode=%22DRIVING%22

For example this link generate the following:

"routes" : [
      {
         "bounds" : {
            "northeast" : {
               "lat" : -20.1765204,
               "lng" : 57.6137001
            },
           "southwest" : {
               "lat" : -20.2921672,
               "lng" : 57.4472155
            }
         },
         "copyrights" : "Map data ©2014 Google",
         "legs" : [
            {
               "distance" : {
                  "text" : "24.6 km",
                  "value" : 24628
               },

i want to extract only the distance and display it in android

like image 737
Breaking code Avatar asked Jan 07 '14 19:01

Breaking code


People also ask

How do I use Google Directions API on Android?

Go to the Google Maps Platform > Credentials page. On the Credentials page, click Create credentials > API key. The API key created dialog displays your newly created API key.

Can you Measure direct distance on Google Maps?

On your computer, open Google Maps. Right-click on your starting point. Select Measure distance. To create a path to measure, click anywhere on the map.


1 Answers

To get a distance from a Google Maps you can use Google Directions API and JSON parser to retrieve the distance value.

Sample Method

private double getDistanceInfo(double lat1, double lng1, String destinationAddress) {
            StringBuilder stringBuilder = new StringBuilder();
            Double dist = 0.0;
            try {

            destinationAddress = destinationAddress.replaceAll(" ","%20");    
            String url = "http://maps.googleapis.com/maps/api/directions/json?origin=" + latFrom + "," + lngFrom + "&destination=" + latTo + "," + lngTo + "&mode=driving&sensor=false";

            HttpPost httppost = new HttpPost(url);

            HttpClient client = new DefaultHttpClient();
            HttpResponse response;
            stringBuilder = new StringBuilder();


                response = client.execute(httppost);
                HttpEntity entity = response.getEntity();
                InputStream stream = entity.getContent();
                int b;
                while ((b = stream.read()) != -1) {
                    stringBuilder.append((char) b);
                }
            } catch (ClientProtocolException e) {
            } catch (IOException e) {
            }

            JSONObject jsonObject = new JSONObject();
            try {

                jsonObject = new JSONObject(stringBuilder.toString());

                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");

                Log.i("Distance", distance.toString());
                dist = Double.parseDouble(distance.getString("text").replaceAll("[^\\.0123456789]","") );

            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            return dist;
        }

For details on parameters and more details on what are the different options available, please refer this.

https://developers.google.com/maps/documentation/directions/

like image 181
Prem Avatar answered Oct 05 '22 13:10

Prem