How do we get the shortest distance route from point A to B by default from Google Direction API
Asked Answered
L

3

9

How do we get the shortest distance route from point A to B by default from Google Direction API suggested alternative routes? By default it gives us shortest duration routes depending upon the current traffic conditions. I have noticed that google responds with multiple alternative routes if you turn on "provideRouteAlternatives=true", I was wondering if we could send a parameter to Google API so that it will always return shortest distance route by default

Laura answered 29/1, 2013 at 23:53 Comment(0)
S
0

As Rameshwor has mentioned, the suggested route returned by Google may be optimised for journey time rather than journey distance. If one or more waypoints have been specified then only one route may be returned anyway, but programatically it's best to assume that more than one route will always be returned.

The following example shows a simple way to find the route with the shortest journey distance using jQuery; please note this code isn't optimised but it should work:

var route_options = [];

for (var i = 0; i < response.routes.length; i++)
{
    var route = response.routes[i];
    var distance = 0;

    // Total the legs to find the overall journey distance for each route option
    for (var j = 0; j < route.legs.length; j++)
    {
        distance += route.legs[j].distance.value; // metres
    }

    route_options.push({
        'route_id': i,
        'distance': distance
    });
}

/*
route_options = [
    {route_id:0, distance:35125},
    {route_id:1, distance:22918},
    {route_id:2, distance:20561}
];
*/

// Sort the route options; shortest to longest distance in ascending order
route_options.sort(function(a, b) {
    return parseInt(a.distance) - parseInt(b.distance);
});

/*
route_options = [
    {route_id:2, distance:20561},
    {route_id:1, distance:22918},
    {route_id:0, distance:35125}
];
*/    

var shortest_distance = (route_options[0]['distance'] * 0.001); // convert metres to kilometres

You can then access the "route_id" value to access the correct route in the response object.

Stowage answered 15/7, 2015 at 19:16 Comment(0)
L
-1

By default Google gives us shortest duration routes depending upon the current traffic conditions. I have noticed that google responds with multiple alternative routes if you turn on “provideRouteAlternatives=true”. When you do not ask for alternate routes, you get the most optimized route by default, although this isn’t necessarily optimized for distance. If you’re trying to get the shortest route, then the way to do this would be to have your application evaluate the total distance of each route in the response, and programmatically select the one with the shortest distance. There isn’t a query parameter that you can pass to Google in the request to say “return only the shortest route”.

Check this demo http://webavenue.com.au/demo/google-shortest-distance/route.html

Laura answered 9/7, 2013 at 7:9 Comment(1)
The demo is not available anymoreAdvisable
M
-3

Using the shortest route rather than the fastest route is generally not a good idea in practice.

When the shortest route is not the fastest, it is likely to be a lower quality route in terms of time, fuel efficiency and sometimes even personal safety. These factors are more important to the majority of drivers on the road.

There are a few workarounds that could potentially yield shorter routes, but they have significant drawbacks, so I'd recommend against them:

  1. Request routes in both directions. Directions from A to B may not yield a feasible route from B to A due to situations like one-way streets, turn restrictions and different locations of highway exits. Requesting routes in both directions and taking the shortest route may yield a route that is not usable in one direction.

  2. Request alternative routes. Asking for alternative routes and picking the shortest route can yield a shorter route than that returned by default. However, alternative routes are not generally stable (may change over time as short-term road conditions change) nor guaranteed to include the shortest route. This means that the shortest route may still not be available, and also the shortest route found by this approach may change over time, giving an impression of instability.

Oftentimes I've seen requests for the shortest routes come from use cases where the goal is rather a realistic driving distance at specific times of the day and week, e.g. workers commuting to/from work. In these cases, driving directions can be requested with departure_time set to the relevant time of the day (and day of the week) within a week (in the future, not past) to obtain a route influenced by typical traffic conditions at that time of the day and week.

Note: this is only available if request includes an API key or a Google Maps APIs Premium Plan client ID.

Mutinous answered 7/7, 2016 at 10:11 Comment(3)
Why spend so much time explaining that the fastest route is the better one, when the question clearly asks for a way to get the shortest route? The argument that the fastest route is safer does not hold true at all, unless you're in New York City or somewhere really well known. I have often seen the fastest route take me through really dodgy areas! Waze does however have an option to request the shortest route by default. It's also a Google product now, so use that to get the shortest route with the same maps.Libelous
Agreed, there are plenty of valid reasons to request the shortest route, as opposed to fastest, for example if you are traveling by other modes (walking, biking, etc. you will prefer the shortest route), but even if you are driving, if you are paying by the mile, you might prefer the shortest route.Disenthrall
For many commercial applications (and particularly where there is specific legislation to require it), it may be mandatory to calculate the shortest route for pricing purposes.Gesellschaft

© 2022 - 2024 — McMap. All rights reserved.