Javascriptの非同期性とGoogleマップに問題があります。 GoogleマップV3 tilesloaded
リスナーを使用して、マーカーにマーカーを追加しています。JavaScript&Google Maps v3:tilesloaded()イベントの結果をキャプチャしますか?
これらのマーカーを取得する方法がわからないので、後でそれらを使って操作できます。なぜなら、tilesloadedは非同期的に発生するため、私はJavascript初心者です!それはすべてのマーカーをロードする前に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を示しています
は、ここでは、コードです。私は追加された余分なマーカーを手に入れることができません。
どうすればこの問題を解決できますか?
ありがとうございました。