我需要一种方法来找出使用Google Maps API的点之间的最佳路线。
假设我有四个点A,B,C和D,并且我从A点开始,需要弄清楚我应该访问的最佳序列,并在地图上绘制最小路径的点。序列
根据我在API文档中研究的内容,很容易找到多个点之间的最低路径,但API需要点的顺序。在我的情况下,我需要API来返回最佳订单。
我认为解决方案是找到所有可能的方法,然后找到所有这些方法的最短路径然后显示这条路线。但是这个解决方案不会有很好的性能,Google API限制了一天内可以绘制的路径数量。
答案 0 :(得分:3)
you may pass optimizeWaypoints: true within the DirectionsRequest to allow the Directions service to optimize the provided route by rearranging the waypoints in a more efficient order. (This optimization is an application of the Travelling Salesman Problem.) All waypoints must be stopovers for theDirections service to optimize their route.
fiddle with {optimizeWaypoints: false}
var start = "New York, NY";
var end = "New York, NY";
var waypts = [];
var wayptsIn = ["Montreal, QBC", "Toronto, ONT", "Chicago,IL", "Winnipeg,MB", "Fargo,ND", "Calgary,AB", "Spokane,WA"];
for (var i = 0; i < wayptsIn.length; i++) {
waypts.push({
location: wayptsIn[i],
stopover: true
});
}
var request = {
origin: start,
destination: end,
waypoints: waypts,
optimizeWaypoints: false,
travelMode: google.maps.TravelMode.DRIVING
};
same fiddle with optimizeWaypoints: true}
var start = "New York, NY";
var end = "New York, NY";
var waypts = [];
var wayptsIn = ["Montreal, QBC", "Toronto, ONT", "Chicago,IL", "Winnipeg,MB", "Fargo,ND", "Calgary,AB", "Spokane,WA"];
for (var i = 0; i < wayptsIn.length; i++) {
waypts.push({
location: wayptsIn[i],
stopover: true
});
}
var request = {
origin: start,
destination: end,
waypoints: waypts,
optimizeWaypoints: true,
travelMode: google.maps.TravelMode.DRIVING
};
答案 1 :(得分:1)
研究您的数据以获取线索...... 这个问题被称为“旅行推销员问题”,并且没有比你提到的蛮力穷尽搜索方法更好的已知方法[查找计算机科学方面的'NP-complete']。 只有当您使用关于路线的“额外”知识(例如Google预期的订单)时,您才可以打败系统,您可以从“内部”应用知识中获得这些知识。只有这样才能让您有机会减少搜索次数。 祝好运!
答案 2 :(得分:1)
使用此optimizeWaypoints:true
https://developers.google.com/maps/documentation/javascript/directions#Waypoints