所以我有一个包含带有(几百个)标记的地图的应用程序。我使用Google提供的markerclusterer.js来聚类我的标记,让整个事情更容易看。我正在使用API V3。
我对所有这一切都很好。但我想要做的是当markerclusterer完成聚类所有标记时执行一些操作。我试过自己做但不看。
有什么建议? (我假设这比我想象的要容易,我的大脑只是油炸)
我只是想知道同样的事情。我就是这样做的:
google.maps.event.addListener(markerClusterer, 'clusteringend', myFunction);
地图"idle"活动会为你工作吗?它应该在MarkerClusterer完成后触发(假设您在页面加载时加载MarkerClusterer)。
看来这个库没有得到维护。多年来它一直无法正常使用v3,也没有开发出任何解决方案。
所以,我下载了非最小化版本并将其放入我正在工作的项目的源代码中。如果您直接在库中进行以下更改,则接受的解决方案仅适用于(适用于v3):
找到createClusters_函数在for循环后添加google.maps.event.trigger(this, "clusteringend", this);
。所以它将如下所示:
MarkerClusterer.prototype.createClusters_ = function() {
if (!this.ready_) {
return;
}
// Get our current map view bounds.
// Create a new bounds object so we don't affect the map.
var mapBounds = new google.maps.LatLngBounds(this.map_.getBounds().getSouthWest(),
this.map_.getBounds().getNorthEast());
var bounds = this.getExtendedBounds(mapBounds);
for (var i = 0, marker; marker = this.markers_[i]; i++) {
if (!marker.isAdded && this.isMarkerInBounds_(marker, bounds)) {
this.addToClosestCluster_(marker);
}
}
google.maps.event.trigger(this, "clusteringend", this);
};
现在你可以使用:
google.maps.event.addListener(markerClusterer, 'clusteringend', myFunction);