0
2 초마다 마커를 업데이트하는지도가 있습니다. 제 3 자 라이브러리를 사용하여 토스트 메시지를 보여줍니다. 특정 마커에서 '무언가'가 잘못되었습니다. 토스트를 클릭하면지도가 중앙에 오르고 특정 마커로 확대되기를 원합니다.자바 스크립트 : 클릭 이벤트를 루프로 지정
하나의 마커는 모두 정상적으로 작동하지만 토스트가 여러 개인 경우 마지막 마커 ('무언가'가 잘못 되었음)가 모두 표시됩니다. 이 문제는 js 클로저 및 범위와 관련된 문제임을 이해하고 있지만 해결 방법을 찾을 수는 없습니다.
if(/*something is wrong at marker*/) {
if(toastShown.indexOf(i) == (-1)) // check if toast has been shown
{
toastShown.push(i); // mark toast for current marker as shown
var err = "Problem detected! Click to go to location";
toastr.error(err, 'Error!', {timeOut: 10000});
problemAtPoint.push(point);
index++;
}
}
for(var j = 0; j < index; j++) {
toastr.options.onclick = (function() {
return function() {
//alert('clicked!');
map.setCenter(problemAtPoint[index]);
map.setZoom(15);
}
})(problemAtPoint[index]);
}
루프에 'j'iterator를 사용하지 않고 onclick 이벤트를 수행합니다. toastr.options가 배열 요소라면 toastr.options [j] – webkit