为什么 Google 地图显示两点之间的持续时间错误?

Why Google maps show wrong duration between of two point?

我的地图上有 4 个不同的点。但它通常 response 对所有 legs 相同 duration。我认为不要用流量来估计。例如,在 google 地图中,我检查了两个点的持续时间。那有时显示1小时8分钟,有时显示1小时7分钟。但在我的项目中它总是 58 分钟。如何解决这个问题并正确显示持续时间?

 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);

根据 Google 地图路线 API 服务的官方文档,只有满足以下所有条件时才会考虑交通信息:

  • The travel mode parameter is driving, or is not specified (driving is the default travel mode).
  • The request includes a valid departure_time parameter. The departure_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|...

来源:https://developers.google.com/maps/documentation/directions/overview#DirectionsAdvanced

查看您的代码,我可以看出您使用了中途停留 waypoints

waypts.push({
    location: companies[i].Address,
    stopover: true
});    

如果您想考虑 real-time 交通状况,您应该将 waypoints 的中途停留 属性 设置为 false。但是,请注意,在这种情况下,您不能使用 waypoints 优化,因为根据官方文档,waypoints 优化仅适用于中途停留:

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.

来源:https://developers.google.com/maps/documentation/directions/overview#OptimizeWaypoints

因此,您必须在交通状况和 waypoints 优化之间做出选择。这两件事不可能同时发生。