【问题标题】:Fire event after MarkerClusterer has finishedMarkerClusterer 完成后触发事件
【发布时间】:2012-09-24 13:44:21
【问题描述】:

所以我有一个应用程序,其中包含一个带有(数百个)标记的地图。我使用 Google 提供的 markerclusterer.js 来聚集我的标记并使整个事情更容易查看。我正在使用 API V3。

我对这一切都很好。但是我想做的是在markerclusterer完成所有标记的聚类后执行一些操作。我试过自己做,但没有看。

有什么建议吗? (我假设这会比我想象的要容易,而且我的大脑只是油炸了)

【问题讨论】:

    标签: google-maps google-maps-api-3 markerclusterer


    【解决方案1】:

    这个库似乎没有得到维护。多年来,它无法与 v3 一起正常工作,也没有开发出任何解决方案。

    所以,我下载了非最小化版本并将其放入我正在工作的项目的源代码中。如果您直接在库中进行以下更改,则接受的解决方案仅适用(对于 v3):

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

    【讨论】:

    【解决方案2】:

    我只是想知道同样的事情。我就是这样做的:

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

    【讨论】:

    • API V3 - 不工作
    【解决方案3】:

    地图"idle" 活动对您有用吗?它应该在 MarkerClusterer 完成后触发(假设您在页面加载时加载 MarkerClusterer)。

    【讨论】:

    • 好吧,我正在使用地图的“空闲”事件来执行 ajax 请求并为标记拉入数据,然后标记群集器正在做这件事。到那时我想收到并举办活动。