From f875421fc43e50aff7d13aca348e4008a93a2727 Mon Sep 17 00:00:00 2001 From: danzel Date: Thu, 22 Nov 2012 17:05:44 +1300 Subject: [PATCH] Fix up some bugs that happen when removing and adding a MarkerClusterGroup from the map. Was leaving layers in _layers which meant they'd get added again when we were re-added. Wasn't unbinding our events from almost anything. Fixes #94 --- src/MarkerClusterGroup.js | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/MarkerClusterGroup.js b/src/MarkerClusterGroup.js index b631968d4..7e24d8ba5 100644 --- a/src/MarkerClusterGroup.js +++ b/src/MarkerClusterGroup.js @@ -321,6 +321,8 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({ this._map.off('zoomend', this._zoomEnd, this); this._map.off('moveend', this._moveEnd, this); + this._unbindEvents(); + //In case we are in a cluster animation this._map._mapPane.className = this._map._mapPane.className.replace(' leaflet-cluster-anim', ''); @@ -329,6 +331,13 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({ } L.FeatureGroup.prototype.onRemove.call(this, map); + + //Clean up all the layers we added to the map + for (var i in this._layers) { + if (this._layers.hasOwnProperty(i)) { + L.FeatureGroup.prototype.removeLayer.call(this, this._layers[i]); + } + } }, @@ -485,6 +494,23 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({ } }, + _unbindEvents: function () { + var spiderfyOnMaxZoom = this.options.spiderfyOnMaxZoom, + showCoverageOnHover = this.options.showCoverageOnHover, + zoomToBoundsOnClick = this.options.zoomToBoundsOnClick, + map = this._map; + + if (spiderfyOnMaxZoom || zoomToBoundsOnClick) { + this.off('clusterclick', null, this); + } + if (showCoverageOnHover) { + this.off('clustermouseover', null, this); + this.off('clustermouseout', null, this); + map.off('zoomend', null, this); + map.off('layerremove', null, this); + } + }, + _zoomEnd: function () { if (!this._map) { //May have been removed from the map by a zoomEnd handler return;