Google Directions Api 返回 0 条路线 [英] Google Directions Api returning 0 routes

查看:24
本文介绍了Google Directions Api 返回 0 条路线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

失败的调用是:

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

<块引用>

警告: 使用 via: 前缀来避免中途停留会导致对航路点的解释非常严格.如果 Google Maps Directions API 无法通过该点创建路线,这可能会导致路线上的严重绕路或响应状态代码中出现 ZERO_RESULTS.

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

我希望这能解释你的疑惑!

We are calling the google directions api to calculate round trip values. In general it works perfectly. I have however come across a use case where it fails to come up with any route. However when we use the js google.maps.DirectionsService version with the same origin, destination, waypoints, and travelMode it works.

The failing call is: 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=

The response is

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

解决方案

When you use via: prefix (no stopover), it adds some additional restrictions. Particularly the U-turn maneuver is not allowed, the route must go straight forward through waypoint. If this is impossible the Directions service will return ZERO_RESULTS.

To check this hypothesis I created exactly the same request, but with stopover (without via: prefix). You can see the result in the Directions calculator:

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

Indeed, you should do the U-Turn in -33.9403,18.666731 (marker B) and this is the reason for ZERO_RESULTS when you try to create a route without stopovers.

This is also confirmed in the official documentation:

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

I hope this explains your doubt!

这篇关于Google Directions Api 返回 0 条路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆