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;