我正在使用 marker clusterer plus library 制作一个 webapp,我使用的代码是:
map = new google.maps.Map(document.getElementById('map'), {
zoom: 17,
center: center,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
markers=[]
for (var i = 0; i < puntos.length; i++) {
punto=puntos[i]
posicion = new google.maps.LatLng(punto[0],punto[1]);
marker = new google.maps.Marker({position: posicion});
markers.push(marker);
}
markerCluster = new MarkerClusterer(map, markers,{ maxZoom:20,gridSize:45,averageCenter:true});
console.log(markerCluster.clusters_)
问题是它在控制台中打印了 markerCluster 对象,但它是空的,没有簇,当我在控制台中再次调用它时它已经在工作了所以我的问题是是否已经初始化了对象 markerCluster当我从代码中调用它时,我可以从我的代码中使用它的集群。
最佳答案
集群是异步的。如果您监听“clusteringend”事件,集群将可用:
google.maps.event.addListener(markerCluster, "clusteringend", function(mc) {
document.getElementById('info').innerHTML += mc.getClusters().length+ " clusters<br>";
});
关于javascript - Marker Clusterer Plus 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17672295/