Google 地图 V3 中没有设定目的地的最短路线?
所以我只是学习 javascript 来搞乱 Google 地图 API。我想知道是否有人对我遇到的这个问题有一个优雅的解决方案。
Google 地图路线请求必须包含三项内容(出发地、目的地和旅行模式)。我的旅行模式将始终是驾驶。原点始终是用户所在的位置。
但目的地需要有所不同。我有几个航路点,用户将访问,并希望根据选择的航路点和用户所在位置提供可能的最短行程,在其中一个航路点结束路线(例如:ABC 或 ACB,但始终为 Axx. ..x)。
除了计算每条可能的路径并查看哪一条距离最短(或时间,或我正在评估的任何内容)之外,还有其他可能的方法吗?看起来这成本会高得令人望而却步(O(n!))。
编辑:将建议的 optimizationWaypoints 标志设置为 true 后,这将成为 O(n) 问题而不是 O(n!) 问题,但现在我遇到了在太短的时间内发出太多请求的问题。
So I'm just learning javascript to mess with the Google Maps API. I was wondering if anyone had an elegant solution to this problem I'm running into.
A Google Maps route request must contain three things (origin, destination, and travelMode). My travelMode will always be DRIVING. The origin will always be wherever the user is located.
The destination though, needs to vary. I have several waypoints and the user will visit, and would like to provide the shortest trip possible depending on what waypoints are selected and where the user is, ending the route at one of the waypoints (eg: ABC or ACB, but always Axx...x).
Is there any possible way to do this other than calculating every possible path and seeing which has the shortest distance (or time, or whatever I'm evaluating on)? It seems like that would be prohibitively costly (O(n!)).
edit: With the suggested optimizeWaypoints flag set to true this becomes a O(n) problem instead of O(n!), but now I have issues with issuing too many requests in too short of a time period.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
谷歌路线中有一个设置可以提供优化路线(optimizeWaypoints - http ://code.google.com/apis/maps/documentation/javascript/services.html#Directions )您只需在路线对象中将其设置为 true
There is a setting in google directions to provide optimized route (optimizeWaypoints - http://code.google.com/apis/maps/documentation/javascript/services.html#Directions ) you simply set it to true in your directions object
如果您想要最短路线,您可以首先调用 Google distanceMatrix API 并获取停靠点的排序列表。
然后使用排序列表调用 API 指令。
If you want the shortest route you can call first to Google distanceMatrix API and get the sort list of stops.
Then call to API directions with the sort list.
一个简单的解决方案是将出发地和目的地指定为相同的,并要求 Google 地图根据所有其他航点优化路线。它通常会首先到达最远点,或者最后到达最远点,然后返回原点。然后,您可以在不优化的情况下发出另一个请求,这次将目的地指定为最后一个优化的航路点。
A simple solution would be to specify Origin and Destination as the same and ask Google Maps to optimize the route based on all other waypoints. It will usually either go to the farthest point first, or last, then come back to the origin. You could then make another request without optimizing, this time specifying the Destination as the last optimized waypoint.