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
This commit is contained in:
danzel
2012-11-22 17:05:44 +13:00
parent f00019b288
commit f875421fc4
+26
View File
@@ -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;