0

How to get total travelled distance and route from google map directions API to show user travelled path like Uber.

Soumyaansh
  • 8,626
  • 7
  • 45
  • 45

1 Answers1

5

You can use the Google Maps Distance Matrix API that provides travel distance and time for a matrix of origins and destinations. It returns information based on the recommended route between start and end points, as calculated by the Google Maps API, and consists of rows containing duration and distance values for each pair.

A Google Maps Distance Matrix API request takes the following form: https://maps.googleapis.com/maps/api/distancematrix/outputFormat?parameters where outputFormat may be either of the following values:

  • json (recommended), indicates output in JavaScript Object Notation (JSON); or
  • xml, indicates output as XML.

Check this example from GitHub. The tutorial finds the distance and time taken to travel in driving mode and by avoiding tolls from origin to destination using Google Maps Distance Matrix API.

Here also are some related SO questions:

Hope this helps!

Community
  • 1
  • 1
abielita
  • 13,147
  • 2
  • 17
  • 59
  • Hello,Thanks for your answer I am using Location api and on onLocationChanged method getting all lat,long on every 10 seconds Interval storing it on database but it will be hundreds of data so for every pair i need to call Distance Matrix API? or i can get distance between two points from this link [link](http://stackoverflow.com/questions/22577075/calculating-the-distance-between-two-latitude-and-longitude-points-in-android) can you suggest me...please – Harshank Sananse Jul 06 '16 at 07:04
  • 1
    After long research i found fix solution which is using distancematrix api as you have told but the tricky thing is we have to pass origins and destinations that is ---22.9942087%2C72.6281171%7C23.027252%2C 72.507454%7C--- so after latitude have to append "%2C" and after longitude have to append "%7C". for more info [link](https://developers.google.com/maps/documentation/distance-matrix/intro#RequestParameters) – Harshank Sananse Jul 06 '16 at 09:57