如何通过来自 google 地图 api 的流量获取导航持续时间?

How to get navigation duration with traffic from google maps api?

我想通过 http 请求从 google 地图 api 获取导航持续时间: https://maps.googleapis.com/maps/api/directions/json?origin=src&destination=destination

但我得到了没有流量的持续时间。 如何获得流量持续时间?

来自 Google 方向 API,其中指出:

duration_in_traffic indicates the total duration of this leg, taking into account current traffic conditions. The duration in traffic will only be returned if all of the following are true:

1.The directions request includes a departure_time parameter set to a value within a few minutes of the current time.
2.The request includes a valid Google Maps API for Work client and signature parameter.
3.Traffic conditions are available for the requested route.
4.The directions request does not include stopover waypoints.

因此您需要满足上述所有要求才能检索有流量的持续时间。

有关详细信息,您可以访问 Google 方向 API 文档的 the Leg section

您现在可以在没有高级帐户的情况下获得 duration_in_traffic(只需来自 the developer console 的标准 API 密钥并启用 "Google Maps Directions API")。在您的浏览器中使用

进行验证

https://maps.googleapis.com/maps/api/directions/json?origin=Brooklyn&destination=Queens&departure_time=now&key=YOUR_KEY_HERE

javascript 客户端似乎使用了另一个尚未 return duration_in_traffic 的端点。

请求必须满足以下条件,更新于 the docs

  • The request includes a departure_time parameter.
  • The request includes a valid API key, or a valid Google Maps APIs Premium Plan client ID and signature.
  • Traffic conditions are available for the requested route.
  • The request does not include stopover waypoints.
  • The request is specifically for driving directions— the mode parameter is set to driving.