问题描述
错误说明:
每当我使用googlemaps库优化路线并使用directions()函数将其绘制在地图上时,结果都会出错。实际上,我已经找到一种解决方法来获得正确的结果,但是它要求更多的请求,对于该付费应用程序,这会导致更多的花费。
首先,获得整个路线持续时间时,最简单的方法(存在错误)是使用directions()
调用optimal_route = gmaps_client.directions(start_coords,end_coords,waypoints(intermediate coords),optimize_waypoints = True)
函数。然后,您可以对路线的每个部分求和,以获得整个轨迹的持续时间和距离:
import googlemaps gmaps_client = googlemaps.Client(key=myapikey) optimal_route = gmaps_client.directions(start_coords,optimize_waypoints = True) legs = optimal_route[0]['legs'] for leg in legs: distance = distance + legs[0]['distance']['value'] duration = duration + legs[0]['duration']['value']
与实际使用电话应用程序并尝试相同的最佳航路点(即使您使用traffic_model = 'best_guess'
)相比,这些距离和持续时间总会产生更大的数字。实际上,如果您执行optimal_route[0]['waypoint_order']
,实际上将获得轨迹上地址的最佳顺序,而不仅仅是正确的持续时间和距离。这是第一个错误。
第二个类似,当您尝试使用plotmap.directions(start_coords,waypoints=waypoints_list,optimize_waypoints = True,traffic_model = 'best_guess',departure_time=datetime.Now())
在地图上进行绘制时,您将获得所有在地图上乱码的绘制线。代码为:
import gmplot plotmap = gmplot.GoogleMapPlotter(coord[0],coord[1],13,apikey=myapikey) plotmap.directions(start_coords,traffic_model ='best_guess',departure_time=datetime.Now()) plotmap.draw('map.html')
解决方法:
我发现要获得正确的路线时间和距离并以有组织,优化和流畅的方式正确绘制路线图的解决方法是,首先要做gmaps_client.directions()
只是为了获得{{ 1}},其结果将得到类似于correct_order = optimal_route[0]['waypoint_order']
的列表。然后按照此特定顺序重新组织[0,4,1,3,2]
,然后通过waypoints_list
进行迭代,并对路线的每个部分进行new waypoints_list
,求和距离和持续时间,如下所示:
gmaps_client.directions() and plotmap.directions()
现在,这将返回具有最佳轨迹,持续时间和距离等于(或实际上接近)Google Maps Phone App中提到的数字的组织化地图,我们知道这是非常准确且接近现实的。但是随后需要更多请求。 无论如何,我觉得我在优化和绘制路线的过程中花了比我应做的更多的钱,无法理解这个主要功能将如何出现这样的错误,以及为什么我找不到互联网上有关此问题的相关信息。
希望我能把问题弄清楚,如果有人能帮助我解决这个问题,我将不胜感激:)
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)