【问题标题】:Array lost after adding all Google Maps markers添加所有 Google 地图标记后数组丢失
【发布时间】:2013-10-09 15:55:08
【问题描述】:

对于 Google 地图项目,我将所有标记加载到一个数组中,以便在将所有标记放在地图上之后将其用于 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 时,标记数组是空的并且没有内容。

有人知道这里发生了什么吗?

【问题讨论】:

  • 会不会与 AJAX 是异步的有关?

标签: javascript arrays google-maps-api-3


【解决方案1】:

loadMarkers 上,您调用startLoadingMarkers 并且这样做:

markers = [];

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多