There are 4 different points in my map. But it generally response
same duration
for all legs
. I think don't estimate with traffic. For example, In google Maps I check two point duration. That show 1 hour 8 minutes sometimes and sometimes show 1 hour 7 minutes. But it always 58 minute in my project. How can solve this problem and show duration correctly?
function calculateAndDisplayRoute(directionsService,directionsRenderer, map, myCompany, companies) {
var waypts = [];
for (var i = 0; i < companies.length; i++) {
waypts.push({
location: companies[i].Address,
stopover: true
});
}
directionsService.route(
{
origin: document.getElementById("start").title,
destination: document.getElementById("start").title,
waypoints: waypts,
optimizeWaypoints: true,
travelMode: "DRIVING",
drivingOptions: {
departureTime: new Date(Date.now()),
trafficModel: 'optimistic'
}
},
function (response, status) {
if (status === "OK") {
console.log("response = ", response)
directionsRenderer.setDirections(response);
var route = response.routes[0];
console.log("route = ", route);
renderDirectionsPolylines(response, map, myCompany, companies);
According to the official documentation of Google Maps Directions API service the traffic information is taken into account only when all following conditions are true:
- The travel mode parameter is driving, or is not specified (driving is the default travel mode).
- The request includes a valid
departure_time
parameter. Thedeparture_time
can be set to the current time or some time in the future. It cannot be in the past.- The request does not include stopover waypoints. If the request includes waypoints, prefix each waypoint with via: to influence the route but avoid stopovers. For example, &waypoints=via:San Francisco|via:Mountain View|...
source: https://developers.google.com/maps/documentation/directions/overview#DirectionsAdvanced
Looking at your code I can see that you use stopover waypoints
waypts.push({
location: companies[i].Address,
stopover: true
});
If you want take into account real-time traffic conditions you should set stopover property of your waypoints to false. However, be advised that in this case you cannot use waypoints optimization, because waypoints optimization works with stopovers only as per official documentation:
By default, the Directions service calculates a route through the provided waypoints in their given order. Optionally, you may pass optimize:true as the first argument within the waypoints parameter to allow the Directions service to optimize the provided route by rearranging the waypoints in a more efficient order. (This optimization is an application of the traveling salesperson problem.) Travel time is the primary factor which is optimized, but other factors such as distance, number of turns and many more may be taken into account when deciding which route is the most efficient. All waypoints must be stopovers for the Directions service to optimize their route.
source: https://developers.google.com/maps/documentation/directions/overview#OptimizeWaypoints
So, you have to choose between traffic conditions and waypoints optimization. Both things are not possible at the same time.