googlearchive/js-marker-clusterer

.setMap(null) error

Opened this issue ยท 6 comments

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)

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

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

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

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

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.

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

yepes commented

@longcui genius!!!