Search code examples
google-distancematrix-api

Can we have different distance for single source and destination?


On Monday, August 8, 2022 20:37:30(IST) and Sunday, August 14, 2022 17:02:32(IST), when we called Google Distance Matrix API for following source and destination address combination

origin_addresses: 143 River St, Jaffrey, NH 03452, USA

destination_addresses: 75 Lowell Rd, Salem, NH 03079, USA

We got different Response as following:

August 8, 2022 20:37:30- We got 101 km

August 14, 2022 17:02:32(IST) - We got 85.9 km

We don't have any extra query parameters except source an destination API URL= Google API URL

  1. Can we know, Is it possible to have different distance for single source and destination?

Solution

  • The short answer is NO. That is, if you are using Distance Matrix API. The API will only return a single route, which is the best possible route.

    But it is possible to have different distance for a single origin and destination by including the alternatives parameter and setting it to true using Directions API.

    I commented on your question as to why it is recommended to use Directions API if you only have a single origin and a single destination. Or watch this Youtube Video for a quick explanation about Distance Matrix API and it's difference with Directions API.

    With that said, I tried the address you used on the Directions API and included the parameter alternatives and set it to true and it showed an array of three alternate routes:

    Directions Result (alternate routes)

    Results are as follows:

    Route #1

    distance: 101 km
    duration: 1 hour 20 mins

    Route #2

    distance: 78.6 km
    duration: 1 hour 22 mins

    Route #3

    distance: 85.9 km
    duration: 1 hour 20 mins

    This concludes that the results you got on August 8 was the Route #1 and the one from August 14 was the Route #3.

    The most probable cause of this could be the almost the same duration. although duration in hours is the same, if you look at the value on the results, route #1 had a value: 4794 while route #3 had a value: 4793. And it could be interchanged because of traffic data and other factors, given that you had two different date and time on your requests(duration in traffic could be different on different days and different times of the day).

    Here's what the docs says:

    "The API returns the most efficient routes when calculating directions. Travel time is the primary factor optimized, but the API may also take into account other factors such as distance, number of turns and many more when deciding which route is the most efficient."

    Here's my request if you need some reference: https://maps.googleapis.com/maps/api/directions/json?units=metric&origin=143%20River%20St,%20Jaffrey,%20NH%2003452,%20USA&alternatives=true&destination=75%20Lowell%20Rd,%20Salem,%20NH%2003079,%20USA&key=YOUR_API_KEY

    Just use your own API key and it should work.

    I hope this helps.