자바 스크립트의 비동기 성격 및 Google지도에 문제가 있습니다. Google Maps V3 tilesloaded
수신기를 사용하여지도에 마커를 추가하고 있습니다.JavaScript 및 Google Maps v3 : tilesloaded() 이벤트의 결과를 캡처합니까?
이 마커를 어떻게 잡을 지 모르겠으므로 tilesloaded가 비동기 적으로 실행되기 때문에 나중에 해당 작업을 수행 할 수 있습니다. 그리고 나는 자바 스크립트 초보자입니다! 이 모든 마커를로드하기 전에 show_place
가 반환되기 때문에,
function show_place() {
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
var marker = addMarker(lat, lng);
markers.push(marker);
// When the map boundaries move, add new markers within the bounds.
google.maps.event.addListener(map, 'tilesloaded', function() {
var bounds = map.getBounds();
$.getJSON("/markers_within_bounds/", { [variables] }, function(json){
for (i=0;i<json.length;i++) {
var map_marker = addMarker(json[i].lat, json[i].lng);
markers.push(map_marker); //Try to add markers to the array. Fail.
}
});
});
return markers;
}
$(document).ready(function() {
var markers = show_place(lat, lng);
alert(markers.length);
// Do things with the markers... if there is more than 1.
});
경고는 항상 1을 보여줍니다
여기에 코드입니다. 추가 된 추가 마커를 확보 할 수 없습니다.
어떻게해야합니까?
감사합니다.