【问题标题】:How to show/hide a MarkerCluster in google maps v3?如何在谷歌地图 v3 中显示/隐藏 MarkerCluster?
【发布时间】:2011-05-02 21:27:17
【问题描述】:

我需要为不同的mapTypes 设置不同的标记,我将它们推送到MarkerClusterer

我“隐藏”标记:

cluster.set("map", null);
cluster.resetViewport();
cluster.redraw();

然后“展示”他们:

cluster.set("map", MAP);
cluster.resetViewport();
cluster.redraw();

问题是 MarkerClusterer 似乎不喜欢set("map", null);它抛出错误TypeError: Object #<a MarkerClusterer> has no method 'remove'。如何以正确的方式显示/隐藏它们?

【问题讨论】:

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


【解决方案1】:

这是我在地图上轻松显示/隐藏集群的代码(针对当前版本的 Maps API 和 JS-Cluster-Renderer 进行了更新)。谢谢加比。

MarkerClusterer.prototype.remove = function() {};

MarkerClusterer.prototype.hide = function() {
  this.setMap(null);
  this.resetViewport();
};

MarkerClusterer.prototype.show = function() {
  this.setMap(map); // replace map with your reference to the map object
  this.redraw();
};

// To hide the clusters:
cluster.hide();

// To show the clusters:
cluster.show();

【讨论】:

    【解决方案2】:

    这里有一个更完整的解决方案:

    在.html中添加:

    <div id="map-canvas-hidden" style="display:none"></div>
    <div id="map-canvas-shown" style="width:500px; height:500px"></div>
    

    在 .js 中添加:

    MarkerClusterer.prototype.remove = function() { };
    var HIDDEN_MAP = new google.maps.Map(document.getElementById("map-canvas-hidden"), {});
    var gmap = new google.maps.Map(document.getElementById("map-canvas-shown"), {});
    

    显示集群:

        cluster.setMap(gmap);
        cluster.resetViewport();
        cluster.redraw();
    

    隐藏集群:

        cluster.setMap(HIDDEN_MAP);
        cluster.resetViewport();
        cluster.redraw();
    

    最后,我需要对 markerclusterer.js 进行以下补丁:

    --- markerclusterer.js.orig 2013-12-06 18:02:32.887516000 +0100
    +++ markerclusterer.js  2013-12-06 18:03:25.487516924 +0100
    @@ -620,6 +620,7 @@
      */
     MarkerClusterer.prototype.getExtendedBounds = function(bounds) {
       var projection = this.getProjection();
    +  if (!projection) return null;
    
       // Turn the bounds into latlng.
       var tr = new google.maps.LatLng(bounds.getNorthEast().lat(),
    @@ -657,7 +658,7 @@
      * @private
      */
     MarkerClusterer.prototype.isMarkerInBounds_ = function(marker, bounds) {
    -  return bounds.contains(marker.getPosition());
    +  return bounds ? bounds.contains(marker.getPosition()) : false;
     };
    

    希望对你有帮助

    【讨论】:

      【解决方案3】:

      清理集群的优雅方式

      cluster.clearMarkers();
      

      【讨论】:

      • 你能澄清一下吗?您是指添加扩展,因为在 MarkerClusterer 对象上不存在 clearMarkers。
      • @mikecamimo 你在使用 API v3 吗?然后看documentation
      • clearMarkers() 不仅将它们从地图中删除,还将它们从集群中删除。
      【解决方案4】:

      在 Javascript API v3 中,这样说就足够了:

      clusterer.setMap(null);
      

      如果您将地图设置回现有的地图对象,集群将重新出现。

      clusterer.setMap( this.map );
      

      另外,我建议不要像在您的示例中那样将您的 Clusterer 命名为“cluster”。 MarkerClusterer 包含 Cluster 对象,它们是实际的集群标记,而不是 ClusterER 本身。

      【讨论】:

        【解决方案5】:

        我通过一个小猴子补丁和一个小技巧努力解决了这个问题。我仍在等待一个明确的答案,但是这我的问题的解决方案,所以我也发布了这个:

        MarkerClusterer.prototype.remove = function () {}
        
        [..]
        
        cluster.set("map", HIDDEN_MAP); // remove the clusterer
        cluster.resetViewport();
        cluster.redraw();
        

        【讨论】:

        • 你在哪里声明 'HIDDEN_MAP' ?或者它是一个不存在的变量,所以你不妨传递 'null' ?
        • @Micros 这是(曾经)从未显示过的单独地图。无论如何,这是一个老问题,我什至没有那个代码了,可能从那时起 API 发生了很大变化。感谢您的帮助!
        猜你喜欢
        • 2013-01-31
        • 1970-01-01
        • 2012-02-18
        • 1970-01-01
        • 2013-04-28
        • 1970-01-01
        • 1970-01-01
        • 2011-04-16
        • 1970-01-01
        相关资源
        最近更新 更多