From b7be077ea5b706aaf0d1810584cd88e7200e6742 Mon Sep 17 00:00:00 2001 From: jerry wang Date: Tue, 7 Jun 2016 17:45:41 +0100 Subject: [PATCH] fix .setMap(null) fix MarkerClusterer.setMap(null) - issue #47 1. added MarkerClusterer.onRemove() 2. added MarkerClusterer.listeners to keep track of listeners on map 3. moved listeners (de)registration into MarkerClusterer.setReady_() 4. extend MarkerClusterer.setReady_() to cover following action on .setMap(null) call a. clear clutters[] b. set all markers with null map c. remove event listeners on previous map --- src/markerclusterer.js | 68 ++++++++++++++++++++++++++---------------- 1 file changed, 43 insertions(+), 25 deletions(-) diff --git a/src/markerclusterer.js b/src/markerclusterer.js index 554fcec..6c23e6d 100755 --- a/src/markerclusterer.js +++ b/src/markerclusterer.js @@ -71,7 +71,8 @@ function MarkerClusterer(map, opt_markers, opt_options) { // there is no point going ahead :) this.extend(MarkerClusterer, google.maps.OverlayView); this.map_ = map; - + this.listeners = {}; + /** * @type {Array.} * @private @@ -156,27 +157,6 @@ function MarkerClusterer(map, opt_markers, opt_options) { this.setMap(map); - /** - * @type {number} - * @private - */ - this.prevZoom_ = this.map_.getZoom(); - - // Add the map event listeners - var that = this; - google.maps.event.addListener(this.map_, 'zoom_changed', function() { - var zoom = that.map_.getZoom(); - - if (that.prevZoom_ != zoom) { - that.prevZoom_ = zoom; - that.resetViewport(); - } - }); - - google.maps.event.addListener(this.map_, 'idle', function() { - that.redraw(); - }); - // Finally, add the markers if (opt_markers && opt_markers.length) { this.addMarkers(opt_markers, false); @@ -228,6 +208,14 @@ MarkerClusterer.prototype.onAdd = function() { this.setReady_(true); }; +/** + * Implementaion of the interface method. + * @ignore + */ +MarkerClusterer.prototype.onRemove = function () { + this.setReady_(false); +}; + /** * Implementaion of the interface method. * @ignore @@ -422,7 +410,7 @@ MarkerClusterer.prototype.pushMarkerTo_ = function(marker) { // If the marker is draggable add a listener so we update the clusters on // the drag end. var that = this; - google.maps.event.addListener(marker, 'dragend', function() { + this.listeners.dragend = google.maps.event.addListener(marker, 'dragend', function() { marker.isAdded = false; that.repaint(); }); @@ -527,9 +515,39 @@ MarkerClusterer.prototype.removeMarkers = function(markers, opt_nodraw) { * @private */ MarkerClusterer.prototype.setReady_ = function(ready) { - if (!this.ready_) { - this.ready_ = ready; + if (ready == this.ready_) {return;} + this.ready_ = ready; + if (this.ready_) { + if (!this.map) return; + this.map_ = this.map; + this.prevZoom_ = this.map.getZoom(); + // Add the map event listeners + var that = this; + this.listeners.zoom_changed = google.maps.event.addListener(this.map_, 'zoom_changed', function() { + var zoom = that.map_.getZoom(); + + if (that.prevZoom_ != zoom) { + that.prevZoom_ = zoom; + that.resetViewport(); + } + }); + this.listeners.idle = google.maps.event.addListener(this.map_, 'idle', function() { + that.redraw(); + }); + if(this.markers_ && this.markers_.length){ + for (var i = 0, marker; marker = this.markers_[i]; i++) { + marker.setMap(this.map); + } + } this.createClusters_(); + } else { + this.resetViewport(true); + // remove event listeners on the map + for(var key in this.listeners) { + google.maps.event.removeListener(this.listeners[key]); + delete this.listeners[key]; + } + this.map_ = null; } };