0

对于谷歌地图项目,我将所有标记加载到一个数组中,以便在将所有标记放在地图上之后将其用于 MarkerClusterer。显示了所有标记,但 MarkerClusterer 未聚类。调试后我发现数组是空的,但我不知道为什么。

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 时,标记数组是空的并且没有内容。

有谁知道这里发生了什么?

4

1 回答 1

0

loadMarkers你打电话时startLoadingMarkers,这会:

markers = [];

看起来您在分配之前清空了全局标记MarkerClusterer(map, markers)

于 2013-10-09T07:55:08.160 回答