我正在创建一个函数来接受一个点数组并将它们作为一条路线显示在地图上。
function __RenderRoute(map, points, lineColor, DrawResult, i, callback, waitTime) {
var tmpPoints = points.slice(i, i + 9);
if (tmpPoints.length > 1) {
var request = {
origin: tmpPoints.shift().location,
destination: tmpPoints.pop().location,
waypoints: tmpPoints,
travelMode: google.maps.TravelMode.DRIVING
};
}
var directionsService = new google.maps.DirectionsService();
directionsService.route(request, function (result, status) {
if (status == google.maps.DirectionsStatus.OK) {
var directionsDisplay = new google.maps.DirectionsRenderer({
draggable: false,
markerOptions: {
visible: false
},
polylineOptions: {
clickable: true,
strokeColor: lineColor,
strokeOpacity: .6,
strokeWeight: 6,
zIndex: 5
},
preserveViewport: true
});
directionsDisplay.setMap(map);
directionsDisplay.setDirections(result);
DrawResult.directionDisplays.push(directionsDisplay);
} else {
console.log(status);
console.log("Wait " + waitTime + " miliseconds.");
setTimeout(function () { __RenderRoute(map, points, lineColor, DrawResult, i, callback, waitTime * 2) }, waitTime);
return;
}
DrawResult.directionResults.push(result);
if (i + 9 < points.length) {
__RenderRoute(map, points, lineColor, DrawResult, i + 8, callback, 350);
} else {
if (callback != null) {
callback();
}
DrawResult.completed = true;
}
});
}
但是,由于我的数组总是包含大量的点(通常超过 100 个),因此很难重复调用该函数,因为地图只会冻结以等待先前的请求完成。有没有办法阻止谷歌地图渲染路线,或者在等待再次调用时停止回调函数本身?