Google 方向 Api 返回 0 条路线

Google Directions Api returning 0 routes

我们调用 google 方向 api 来计算往返值。一般来说,它工作得很好。然而,我遇到了一个用例,它无法提出任何路线。但是,当我们使用具有相同起点、目的地、waypoints 和 travelMode 的 js google.maps.DirectionsService 版本时,它可以工作。

失败的调用是: https://maps.googleapis.com/maps/api/directions/json?origin=-33.92873,18.458879&destination=-33.92873,18.458879&waypoints=via:-33.9403,18.666731&mode=driving&key=

响应是

{
   "geocoded_waypoints" : [ {}, {}, {} ],
   "routes" : [],
   "status" : "ZERO_RESULTS"
}

当您使用 via: 前缀(无中途停留)时,它会增加一些额外的限制。特别是不允许掉头机动,航线必须直行通过航路点。如果这是不可能的,路线服务将 return ZERO_RESULTS.

为了验证这个假设,我创建了完全相同的请求,但有中途停留(没有 via: 前缀)。您可以在路线计算器中看到结果:

https://directionsdebug.firebaseapp.com/?origin=-33.92873%2C18.458879&destination=-33.92873%2C18.458879&waypoints=-33.9403%2C18.666731

确实,您应该在 -33.9403,18.666731(标记 B)中掉头,这就是当您尝试创建没有中途停留的路线时 ZERO_RESULTS 的原因。

官方文档中也证实了这一点:

Caution: Using the via: prefix to avoid stopovers results in directions that are very strict in their interpretation of the waypoint. This may result in severe detours on the route or ZERO_RESULTS in the response status code if the Google Maps Directions API is unable to create directions through that point.

https://developers.google.com/maps/documentation/directions/intro#Waypoints

希望这能解释你的疑惑!