Javascript 如何在google maps v3中显示/隐藏MarkerCluster?

Javascript 如何在google maps v3中显示/隐藏MarkerCluster?,javascript,google-maps-api-3,markerclusterer,Javascript,Google Maps Api 3,Markerclusterer,我需要为不同的mapTypes使用不同的标记,我正在将它们推到一个新的位置 我使用以下命令“隐藏”标记: cluster.set("map", null); cluster.resetViewport(); cluster.redraw(); 并“展示”给他们看: cluster.set("map", MAP); cluster.resetViewport(); cluster.redraw(); 问题是MarkerClusterer似乎不喜欢集(“map”,null);它抛出错误TypeE

我需要为不同的
mapType
s使用不同的标记,我正在将它们推到一个新的位置

我使用以下命令“隐藏”标记:

cluster.set("map", null);
cluster.resetViewport();
cluster.redraw();
并“展示”给他们看:

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

问题是MarkerClusterer似乎不喜欢
集(“map”,null)
;它抛出错误
TypeError:Object#没有方法“remove”
。如何以正确的方式显示/隐藏它们?

我通过一个小补丁和一个小黑客来解决这个问题。我仍在等待一个清晰的答案,但这是我问题的解决方案,因此我也发布了以下内容:

MarkerClusterer.prototype.remove = function () {}

[..]

cluster.set("map", HIDDEN_MAP); // remove the clusterer
cluster.resetViewport();
cluster.redraw();

在Javascript API v3中,可以说:

clusterer.setMap(null);
如果将贴图设置回现有贴图对象,则簇将重新出现

clusterer.setMap( this.map );

此外,我建议不要像您的示例中那样,将您的Clusterer命名为“cluster”。MarkerClusterer包含群集对象,这些对象是实际的群集标记,而不是群集器本身。

清除群集的优雅方法

cluster.clearMarkers();

以下是一个更完整的解决方案:

在.html中添加:

<div id="map-canvas-hidden" style="display:none"></div>
<div id="map-canvas-shown" style="width:500px; height:500px"></div>
要显示群集,请执行以下操作:

    cluster.setMap(gmap);
    cluster.resetViewport();
    cluster.redraw();
    cluster.setMap(HIDDEN_MAP);
    cluster.resetViewport();
    cluster.redraw();
要隐藏群集,请执行以下操作:

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

希望这有帮助

以下是我的代码,可以轻松地在地图上显示/隐藏集群(针对当前版本的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();

您在哪里声明“隐藏地图”?或者它是一个不存在的变量,所以您可以传递“null”?@Micros这是一个从未显示过的单独映射。不管怎么说,这是一个老问题,我甚至没有那个代码了,可能API从那时起变化很大。谢谢你的帮助!你能澄清一下吗?您是指添加扩展,因为MarkerClusterer对象上不存在clearMarkers。@MikeCamo您使用的是API v3吗?然后看看clearMarkers(),它不仅从地图中删除了它们,还从集群中删除了它们。
clearMarkers()
不存在!另一个问题的setVisible()答案似乎适合我