3

我已经成功地设置了 MarkerClusterer v3 和视口标记管理(执行 ajax 调用以仅收集视口可见的标记并在地图“空闲”时渲染这些标记)。

但是,当我将它们组合在一起时,它们似乎只在页面第一次加载而不是之后一起工作。

缩放或平移时,保留初始集群,并且整个地图的标记呈现为非集群状态,但保留先前集群的标记。

当您放大/缩小时,原始的聚集标记仍然可以正常运行,但是在更改视口边界时提供的新标记不会添加到它们或聚集在一起。

下面的代码:

function drawMap(swLat, swLng, neLat, neLng){
    //Load the map data
     $.ajax({
            type: "POST",
            url: "readMapInfo.php",
            cache: false,
            data:{S:swLat, W:swLng, N:neLat, E:neLng},
            dataType: "xml",
            success: function(data) {
            if(markerArray.length >0){
                for (i in markerArray) {
                    markerArray[i].setMap(null);
                }
                drawMarker(data);   //takes the info provided and performs "markerArray.push(marker);"
                mc = new MarkerClusterer(map, markerArray, clusterOptions);
            } else {
                drawMarker(data);   //takes the info provided and performs "markerArray.push(marker);"
                mc = new MarkerClusterer(map, markerArray, clusterOptions);
            }
    });
}

google.maps.event.addListener(map, 'idle', function() {
    bounds = map.getBounds();
    sw = bounds.getSouthWest();
    ne = bounds.getNorthEast();
    swLat = sw.lat();
    swLng = sw.lng();
    neLat = ne.lat();
    neLng = ne.lng();

    drawMap(swLat, swLng, neLat, neLng);
});
4

1 回答 1

2

您对问题的描述详细而透彻,但如果还有一个指向显示问题的页面的 URL 会更容易。当然,在这种特定情况下,这可能是不可能的。也就是说,我会尽力帮助:

我相信您在 ajax 成功回调开始时需要一些额外的清理代码:

if( markerArray.length > 0 ) {
    // For loop logic is unchanged
    // It removes the markers from the Map, but leaves them in markerArray
    for (i in markerArray) {
        markerArray[i].setMap( null );    
    }

    // New code to truncate the Array; the markers should be removed
    markerArray.length = 0;

    // New code to clear the clusterer; the markers should be removed
    mc.clearMarkers();        

    // Original line of code unchanged
    drawMarker(data);   //takes the data and performs markerArray.push(marker)

    // Commented out, because the cluster is cleared each time, not recreated
    //mc = new MarkerClusterer(map, markerArray, clusterOptions);

    // New code to refill the cluster, rather than recreate the cluster
    // The original clusterOptions are retained
    mc.addMarkers( markerArray );

} else {
    // Original line of code unchanged
    drawMarker(data);   //takes the data and performs markerArray.push(marker)

    // Original line of code unchanged
    // Necessary here, because the clusterer does not yet exist
    mc = new MarkerClusterer(map, markerArray, clusterOptions);
}

我相信这将有助于您前进。请让我知道这是否可以解决问题或至少有帮助。

在你解决了眼前的挑战之后,我还建议你看看MarkerClustererPlus;问题中对此进行了描述:是否有任何方法可以在少于定义的标记计数时禁用标记聚类器?.

于 2012-05-02T03:05:46.223 回答