Как показать / скрыть MarkerCluster в Google Maps v3? - PullRequest
5 голосов
/ 30 октября 2010

Мне нужны разные маркеры для разных mapType с, и я помещаю их в 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'.Как я могу показать / скрыть их надлежащим образом?

Ответы [ 5 ]

7 голосов
/ 26 сентября 2012

Элегантный способ очистить кластер

cluster.clearMarkers();
6 голосов
/ 22 марта 2012

В Javascript API v3 достаточно сказать:

clusterer.setMap(null);

Если вы вернете карту к существующему объекту карты, кластеры появятся снова.

clusterer.setMap( this.map );

Кроме того, я бы предложил не называть ваш кластер «кластером», как в вашем примере. MarkerClusterer содержит объекты Cluster, которые являются фактическими кластерными маркерами, а не самим ClusterER.

1 голос
/ 19 октября 2016

Вот мой код, позволяющий легко показывать / скрывать кластеры на карте (обновлено для текущих версий 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();
1 голос
/ 06 декабря 2013

Вот более полное решение:

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

надеюсь, это поможет

0 голосов
/ 31 октября 2010

Я попытался решить эту проблему с помощью небольшого обезьяньего патча и небольшого взлома.Я все еще жду чистого ответа, но это является решением моей проблемы, поэтому я также публикую это:

MarkerClusterer.prototype.remove = function () {}

[..]

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