0
나는 포인트 배열을 받아들이고 하나의 경로로 맵에 표시하는 함수를 만들고 있습니다. 내 배열이 항상 점 (보통 100 개 이상)의 엄청난 금액을 포함 반복적으로 단지 완료 이전 요청을 기다릴 밖으로 동결지도로 함수를 호출에렌더링 방향에서 Google지도를 중지하는 방법?
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;
}
});
}
그러나, 그것은 어렵습니다. 어쨌든 Google지도를 렌더링 경로에서 멈추거나 다시 호출 할 때 대기하는 동안 콜백 자체에서 기능을 중지 할 수 있습니까?
이것이 답이라고 생각하지 않지만 'if (tmpPoints.length> 1)'테스트에 실패하면 어떻게됩니까? 'request'는 정의되지 않고'directionsService.route (request, function (result, status) ...'는 외부 범위에서'request'를 받거나 에러를 던질 것입니다 –
사실 tmpPoints.length는 항상> 1입니다. 만약 (tmpPoints.length> 1) 의미가 없다면, 아직 제거하지 않았기 때문에 여전히 그 이유가 있습니다. – vCube
'tmpPoints.length'는 오늘과 내일 그리고 그 다음날보다 클 수도 있지만 항상 * 1보다 커야합니까? [방어 프로그래밍] (http://en.wikipedia.org/wiki/Defensive_programming). –