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

大城市里の小女人 提交于 2019-12-21 04:04:28

问题


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


回答1:


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.




回答2:


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




回答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.



来源:https://stackoverflow.com/questions/14594291/how-do-we-get-the-shortest-distance-route-from-point-a-to-b-by-default-from-goog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!