gpt4 book ai didi

javascript - 为 Google map 方向服务循环多个起点

转载 作者:行者123 更新时间:2023-11-28 06:13:47 25 4
gpt4 key购买 nike

我想找到多个出发地到多个目的地的最短行驶距离。假设我有 5 个顾客和 10 个商店,我想找到每个顾客到商店的最短距离。

我现在的问题是 Google 方向服务每秒 10 次查询的限制。对于每个客户来说,完成查询API的时间不到1秒,所以我会达到每个客户的查询限制。

我尝试在每个客户之间实现延迟,但来自 google 定向服务的回调函数没有被阻止...

  // A function to calculate the route between our current position and some desired end point.
function calcRoute(end, callback) {
var request = {
origin: currentPosition,
destination: end,
travelMode: google.maps.TravelMode.DRIVING
};
directionsService.route(request, function(response, status) {

if (status == google.maps.DirectionsStatus.OK) {
callback(response);
} else {
size--;
}
});
}

// Stores a routing result from the API in our global array for routes.
function storeResult(data) {
routeResults.push(data);
if (routeResults.length === size) {
findShortest();
}
}

// Goes through all routes stored and finds which one is the shortest. It then
// sets the shortest route on the map for the user to see.
function findShortest() {
var i = routeResults.length;
var shortestIndex = 0;
var shortestLength = routeResults[0].routes[0].legs[0].distance.value;

while (i--) {
if (routeResults[i].routes[0].legs[0].distance.value < shortestLength) {
shortestIndex = i;
shortestLength = routeResults[i].routes[0].legs[0].distance.value;
}
}
directionsDisplay.setDirections(routeResults[shortestIndex]);
}

有没有办法在每次迭代后阻止回调?或者还有其他方法可以做到这一点吗?

最佳答案

下面的代码应该可以为您完成这项工作。

// A function to calculate the route between our current position and some desired end point.
function calcRoute(end, callback) {
var request = {
origin: currentPosition,
destination: end,
travelMode: google.maps.TravelMode.DRIVING
};
directionsService.route(request, function (response, status) {
if (status == google.maps.DirectionsStatus.OK) {
callback(response);
}
//Handle the limit of 10 queries per sec
else if (status === google.maps.DirectionsStatus.OVER_QUERY_LIMIT) {
setTimeout(function () {
calcRoute(end, callback);
}, 1100);
}
else {
// a result could not found due to any one of the following errors:
//UNKNOWN_ERROR or REQUEST_DENIED or INVALID_REQUEST or MAX_WAYPOINTS_EXCEEDED
size--;
}
});
}

// Stores a routing result from the API in our global array for routes.
function storeResult(data) {
routeResults.push(data);
if (routeResults.length === size) {
findShortest();
}
}

// Goes through all routes stored and finds which one is the shortest. It then
// sets the shortest route on the map for the user to see.
function findShortest() {
var i = routeResults.length;
var shortestIndex = 0;
var shortestLength = routeResults[0].routes[0].legs[0].distance.value;

while (i--) {
if (routeResults[i].routes[0].legs[0].distance.value < shortestLength) {
shortestIndex = i;
shortestLength = routeResults[i].routes[0].legs[0].distance.value;
}
}
directionsDisplay.setDirections(routeResults[shortestIndex]);
}

关于javascript - 为 Google map 方向服务循环多个起点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36148642/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com