From e7e1d5391f2612c8d8da92a1e310aeaac8d6e2ad Mon Sep 17 00:00:00 2001 From: danzel Date: Fri, 26 Apr 2013 09:58:40 +1200 Subject: [PATCH] Fix #160 If we are added to the map, then removed, then a marker is removed from us. Previously that marker wouldn't be removed, now it is when we get re-added to the map. --- src/MarkerClusterGroup.js | 37 +++++++++++++++++++++++++++---------- 1 file changed, 27 insertions(+), 10 deletions(-) diff --git a/src/MarkerClusterGroup.js b/src/MarkerClusterGroup.js index f0fd78fd3..9f4ae759d 100644 --- a/src/MarkerClusterGroup.js +++ b/src/MarkerClusterGroup.js @@ -41,6 +41,7 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({ this._inZoomAnimation = 0; this._needsClustering = []; + this._needsRemoving = []; //Markers removed while we aren't on the map need to be kept track of //The bounds of the currently shown area (from _getExpandedVisibleBounds) Updated on zoom/move this._currentShownBounds = null; }, @@ -94,7 +95,9 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({ removeLayer: function (layer) { if (!this._map) { - this._arraySplice(this._needsClustering, layer); + if (!this._arraySplice(this._needsClustering, layer) && this.hasLayer(layer)) { + this._needsRemoving.push(layer); + } return this; } @@ -262,12 +265,18 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({ return false; } - if (this._needsClustering.length > 0) { - var anArray = this._needsClustering; - for (var i = anArray.length - 1; i >= 0; i--) { - if (anArray[i] === layer) { - return true; - } + var i, anArray = this._needsClustering; + + for (i = anArray.length - 1; i >= 0; i--) { + if (anArray[i] === layer) { + return true; + } + } + + anArray = this._needsRemoving; + for (i = anArray.length - 1; i >= 0; i--) { + if (anArray[i] === layer) { + return false; } } @@ -315,13 +324,20 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({ //Overrides FeatureGroup.onAdd onAdd: function (map) { this._map = map; + var i, l, layer; if (!this._gridClusters) { this._generateInitialClusters(); } - for (var i = 0, l = this._needsClustering.length; i < l; i++) { - var layer = this._needsClustering[i]; + for (i = 0, l = this._needsRemoving.length; i < l; i++) { + layer = this._needsRemoving[i]; + this._removeLayer(layer); + } + this._needsRemoving = []; + + for (i = 0, l = this._needsClustering.length; i < l; i++) { + layer = this._needsClustering[i]; if (layer.__parent) { continue; } @@ -329,6 +345,7 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({ } this._needsClustering = []; + this._map.on('zoomend', this._zoomEnd, this); this._map.on('moveend', this._moveEnd, this); @@ -377,7 +394,7 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({ for (var i = anArray.length - 1; i >= 0; i--) { if (anArray[i] === obj) { anArray.splice(i, 1); - return; + return true; } } },