Updated 25 September 2021
We are getting distance & duration between two locations in Google map. We will find duration according to different Traveling Modes such as –
driving
(default) indicates standard driving directions using the road network.walking
requests walking directions via pedestrian paths & sidewalks (where available).bicycling
requests bicycling directions via bicycle paths & preferred streets (where available).transit
requests directions via public transit routes (where available).You’ll need to request the directions over HTTP. You can do this directly from Android, or via your own server.
Request the google map server with URL like-
1 |
String url = "https://maps.googleapis.com/maps/api/directions/" + output + "?" + parameters; |
with GET request method.
where “output” is a format of the response and parameter contains values.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
// Origin of route String str_origin = "origin=" + origin.latitude + "," + origin.longitude; // Destination of route String str_dest = "destination=" + dest.latitude + "," + dest.longitude; // Sensor enabled String sensor = "sensor=false"; // Travelling mode enable <strong>String mode = "mode = driving"</strong> // Building the parameters to the web service String parameters = str_origin + "&" + str_dest + "&" + sensor + "&"+ mode; // Output format String output = "json"; |
Connection to communicate Map Server with URL.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
/** A method to download json data from url */ private String downloadUrl(String strUrl) throws IOException { String data = ""; InputStream iStream = null; HttpURLConnection urlConnection = null; try { URL url = new URL(strUrl); // Creating an http connection to communicate with url urlConnection = (HttpURLConnection) url.openConnection(); // Connecting to url urlConnection.connect(); // Reading data from url iStream = urlConnection.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(iStream)); StringBuffer sb = new StringBuffer(); String line = ""; while ((line = br.readLine()) != null) { sb.append(line); } data = sb.toString(); br.close(); } catch (Exception e) { Log.d("Exception while downloading url", e.toString()); } finally { iStream.close(); urlConnection.disconnect(); } Log.d("Data =", "url"+data.toString()); return data; } |
Getting the response from the Google map server and parse response string to JSON format.
Getting distance & duration-
1 2 3 4 5 |
String response = downloadUrl(url); String distance; String duration; distance = response.getJSONArray("routes").getJSONObject(routeIndex).getJSONArray("legs").getJSONObject(index).getJSONObject("distance").getString("text"); duration = response.getJSONArray("routes").getJSONObject(routeIndex).getJSONArray("legs").getJSONObject(index).getJSONObject("duration").getString("text"); |
Sources: Google Maps Directions API
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
plz