5

我需要为不同mapType的 s 设置不同的标记,并且我将它们推送到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'。我怎样才能以正确的方式显示/隐藏它们?

4

5 回答 5

6

在 Javascript API v3 中足以说明:

clusterer.setMap(null);

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

clusterer.setMap( this.map );

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

于 2012-03-22T10:48:51.930 回答
6

清除集群的优雅方式

cluster.clearMarkers();
于 2012-09-26T07:15:18.380 回答
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;
 };

希望这可以帮助

于 2013-12-06T17:15:20.267 回答
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();
于 2016-10-19T14:05:28.210 回答
0

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

MarkerClusterer.prototype.remove = function () {}

[..]

cluster.set("map", HIDDEN_MAP); // remove the clusterer
cluster.resetViewport();
cluster.redraw();
于 2010-10-31T18:40:45.020 回答