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

How can we get the shortest route from point A to B by default from the Google Direction API offered by alternative routes? By default, it gives us routes with the shortest duration, depending on the current traffic conditions. I noticed that google responds with several alternative routes, if you enable "provide RouteAlternatives = true", I was wondering if we can send the parameter to the Google API so that it always returns the route with the smallest distance by default.

+9
source share
3 answers

As Rameshwor mentioned, the proposed route returned by Google can be optimized for travel time rather than travel distance. If one or more waypoints are specified, only one route can be returned in any case, but it is better to assume in software that more than one route will always be returned.

The following example shows an easy way to find the route with the shortest distance using jQuery; note that this code is not optimized, 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 value of โ€œroute_idโ€ to access the correct route in the response object.

0
source

By default, Google provides us with the shortest routes, depending on the current traffic conditions. I noticed that google responds with several alternative routes if you enable "provide RouteAlternatives = true". If you do not request alternative routes, by default you get the most optimized route, although this is not necessarily optimized for distance. If you are trying to get the shortest route, the way to do this would be to have your application estimate the total distance of each route in the answer and programmatically select the one that has the shortest distance. There is no query parameter that you can pass to Google in the query to say "return only the shortest route."

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

-1
source

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

If the shortest route is not the fastest, it will most likely be a better route in terms of time, fuel efficiency and sometimes even personal safety. These factors are more important for most drivers on the road.

There are several workarounds that could potentially lead to shorter routes, but they have significant drawbacks, so I would recommend them:

  • Request routes in both directions. Directions A to B cannot lead to an acceptable route from B to A due to situations such as one-way streets, train restrictions, and the different location of highway exits. Requesting routes in both directions and obtaining the shortest route can give you a route that cannot be used in one direction.

  • Request alternative routes. Asking for alternative routes and choosing the shortest route can lead to a shorter route than the default. However, alternative routes are usually not stable (may change over time as short-term changes in road conditions), and do not guarantee the inclusion of the shortest route. This means that the shortest route may not be available, and the shortest route found by this approach may change over time, giving the impression of instability.

Often I saw that requests for the shortest routes come from use cases where the goal is a fairly realistic distance while driving at certain times of the day or week, for example. workers coming to / from work. In these cases, travel routes can be requested with departure_time set at the appropriate time of the day (and day of the week) during the week (in the future rather than in the past) to obtain a route that is affected by typical traffic conditions at that time of the day and week.

Note : this is only available if the request contains an API key or a Google Maps API plan client code.

-3
source

All Articles