我已经成功设置了MarkerClusterer v3和Viewport Marker Management(执行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);
});
答案 0 :(得分: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;它在问题中描述:Is there any way to disable the Marker Clusterer for less than defined marker counts?。