Google 地图 api 绕过交通的最快路线

Google maps api fastest route around traffic

我有一个 SPA,需要能够在两点之间为用户提供方向。我希望方向的工作方式与他们目前在网络版 google 地图上的工作方式相同。 IE。 https://www.google.ca/maps/dir/ 在由 google 托管的网络版本中,当您请求路线时,考虑到交通情况,它会为您提供最快的路线...(参见屏幕截图)

目前,当我使用以下代码发出请求时,它仅 returns 一条不考虑流量的路由。

var directionsService = this.get('directionsService');
  var directionsDisplay = this.get('directionsDisplay');
  directionsService.route({
      origin: new google.maps.LatLng(this.get('currentLocation.lat'),this.get('currentLocation.lng')),
      destination: new google.maps.LatLng(toAddress.lat,toAddress.lng),
      travelMode: 'DRIVING',
      provideRouteAlternatives: true
    }, function(response, status) {
      if (status === 'OK') {
        directionsDisplay.setDirections(response);
      } else {
        window.alert('Directions request failed due to ' + status);
      }
    });

有谁知道正确的方法吗?

检查 DirectionService.route 返回的路线数量,回调将有一个 DirectionsResult 对象和一个 DirectionsRoute 数组,其中包含有关其组成的腿和步骤的信息。

如果它只返回 1 条路线,请尝试在您的 DirectionsRequest 对象中将 provideRouteAlternatives 设置为 true。

编码愉快!

TyBourque。 我认为当前 google 地图 API 不支持带有交通图层的方向图。 仅在使用嵌入式地图 API.

时可用

如果您已经找到最佳解决方案(不使用嵌入式地图API),请写在这里

此致