2013-10-09 23 views
0

對於Google Maps項目,我將所有標記加載到數組中,以便在將所有標記放置在地圖上之後將其用於MarkerClusterer。顯示所有標記,但MarkerClusterer不是聚類。調試後,我發現數組是空的,但我找不出原因。添加所有Google地圖標記後陣列丟失

var matLocation = null; 
var markers = []; 

//setup the makers 
function loadMarkers() 
{ 
    //load array with markers 
    startLoadingMarkers(); 

    //add markercluster to page so we markers will get clustered 
    var markerCluster = new MarkerClusterer(map, markers); 
    markerCluster.maxZoom_ = 14;   

    //More stuff 
} 


/* 
* Load the markers from the source 
*/ 
function startLoadingMarkers() 
{ 
    //empty markers 
    for(i in markers) 
    { 
     var marker = markers[i]; 
     marker.setMap(null); 
    } 
    markers = []; 

    $.get('load some marker source', function(data) { 
     matLocation = jQuery.parseJSON(data); 
     LoadMarker(1); 
    }); 
} 


/* 
* Load the marker one by one 
*/ 
function LoadMarker(nextIndex) 
{ 
    if(nextIndex < matLocation.locations.length){ 
     var arrItem = matLocation.locations[nextIndex]; 

     //Put the marker on the map (all working fine) 

     //add to array 
     markers.push(marker); 

     //go to next 
     LoadMarker((nextIndex + 1)); 
    } 
    //ARRAY IS STILL FILLED HERE 
} 

該數組在每次迭代中填充並且在所有迭代結束時都有內容。但是當我想啓動MarkerClusterer時,標記數組是空的並且沒有內容。

任何人都知道這裏發生了什麼?

+0

難道是與相關的AJAX是異步的東西之前清空全球標誌? – iTURTEV

回答

0

loadMarkers你打電話startLoadingMarkers這確實:

markers = []; 

看起來你你assing到MarkerClusterer(map, markers)