Search code examples
androidgoogle-mapsandroid-maps-v2pubnub

How to get total travelled distance and route from google map directions api like uber


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


Solution

  • 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!