googlearchive / js-marker-clusterer

A marker clustering library for the Google Maps JavaScript API v3.
https://googlemaps.github.io/js-marker-clusterer/docs/reference.html
Apache License 2.0
1.28k stars 775 forks source link

.setMap(null) error #47

Open thdoan opened 8 years ago

thdoan commented 8 years ago

I get this error when I try to disable the marker clusterer overlay:

var markerCluster = new MarkerClusterer(map, markers);
...
markerCluster.setMap(null);

Console output:

Uncaught TypeError: this.remove is not a function (js?libraries=places:36)
Uncaught TypeError: this.remove is not a function (overlay.js:1)
junalmeida commented 8 years ago

This also affects me. Add the following to line 229.

/**
 * Implementaion of the interface method.
 * @ignore
 */
MarkerClusterer.prototype.onRemove = function () {
    this.setReady_(true);
};
jerrywang121 commented 8 years ago

On top of implementing the onRemove() as suggested junalmeida , you will also need to take care of

  1. clear clutters[]
  2. set all markers with null map
  3. remove event listeners on previous map attached is modified version which works fine for switching on/off markers with setMap function call

markerclusterer.zip

thealjey commented 8 years ago

@jerrywang121 your solution works perfectly! Could you make it into a pull request?

Gert25 commented 6 years ago

My short solution to this problem was to first overwrite the onRemove() function before calling the constructor and nuking the clusters. var mc =window.MarkerClusterer.prototype.onRemove = function(){ for ( var i = 0 ; i < this.clusters_.length; i++){ this.clusters_[i].remove() } } And then instantiate the Markerclusterer mc = new window.MarkerClusterer(map,filteredMarkers, options)

note that filteredMarkers are my google markers and and the options I pass into the constructor options is an imagePath and the maxzoom options. it doesn't effect the way the solution is implemented.

Then finally I call the setMap(null) method on the Markercluster object which fires the onRemove function: mc.setMap(null)

Also not that I remove the google markers from the map (by calling the setMap(null) method on each marker) before calling the mc.SetMap(null) method.

longcui commented 6 years ago

I got the same error, Use markerCluster.clearMarkers(); works

yepes commented 5 years ago

@longcui genius!!!