How to show / hide MarkerCluster in google maps v3?

I need to have different markers for different mapTypes, and I click them on MarkerClusterer .

I will hide the markers with:

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

And "show" them with:

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

The problem is that MarkerClusterer does not seem to be like set("map", null); he gives an error TypeError: Object #<a MarkerClusterer> has no method 'remove'. How can I show / hide them appropriately?

+3
source share
5 answers

I fought in this decision, a little beckoning and a little hacking. I am still waiting for a clean answer, but this is the solution to my problem, so I also post this:

MarkerClusterer.prototype.remove = function () {}

[..]

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

cluster.clearMarkers();
+6

Javascript API v3 :

clusterer.setMap(null);

, .

clusterer.setMap( this.map );

, , . MarkerClusterer , , .

+5

:

.html add:

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

in.js add:

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

,

+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();
+1

Source: https://habr.com/ru/post/1772282/


All Articles