Fire event after MarkerClusterer has finished

佐手、 提交于 2019-12-19 02:31:40

问题


So I have an application that contains a map with (several hundreds) of markers. I use the markerclusterer.js supplied by Google to cluster my markers and make the whole thing easier to look at. I'm using API V3.

I'm fine with all of that. But what I would like to do is perform some action when the markerclusterer has finished clustering all the markers. I've tried to do it myself but with no look.

Any suggestions? (I'm assuming this will be easier than I think and my brain is just fried)


回答1:


I was just wondering the same thing. This is how I did it:

google.maps.event.addListener(markerClusterer, 'clusteringend', myFunction);



回答2:


Will the map "idle" event work for you? It should fire once the MarkerClusterer has finished (assuming you are loading the MarkerClusterer when the page loads).




回答3:


It seems this library is not being maintained. It does not work with v3 properly for years and no solution has been developed.

So, I downloaded the non-minimised version and put it into source code for the projects I am working. Accepted solution will only work(for v3) if you make the below changes directly in the library:

find createClusters_ function add google.maps.event.trigger(this, "clusteringend", this); after the for loop. So it will look like below:

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);
};

Now you can use:

google.maps.event.addListener(markerClusterer, 'clusteringend', myFunction);



来源:https://stackoverflow.com/questions/12711736/fire-event-after-markerclusterer-has-finished

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!