diff --git a/src/MarkerClusterGroup.js b/src/MarkerClusterGroup.js index c741fb6e..1b514655 100644 --- a/src/MarkerClusterGroup.js +++ b/src/MarkerClusterGroup.js @@ -75,6 +75,8 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({ L.extend(this, animate ? this._withAnimation : this._noAnimation); // Remember which MarkerCluster class to instantiate (animated or not). this._markerCluster = animate ? L.MarkerCluster : L.MarkerClusterNonAnimated; + //Set the initial clear layers count to 0 + this._clearLayersCallCount = 0; }, addLayer: function (layer) { @@ -199,13 +201,17 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({ l = layersArray.length, offset = 0, originalArray = true, - m; + m, + clearLayerIndexWhenStarted = this._clearLayersCallCount; if (this._map) { var started = (new Date()).getTime(); var process = L.bind(function () { var start = (new Date()).getTime(); for (; offset < l; offset++) { + if (clearLayerIndexWhenStarted !== this._clearLayersCallCount) { + break; + } if (chunked && offset % 200 === 0) { // every couple hundred markers, instrument the time elapsed since processing started: var elapsed = (new Date()).getTime() - start; @@ -260,6 +266,10 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({ } } + if (clearLayerIndexWhenStarted !== this._clearLayersCallCount) { + return; + } + if (chunkProgress) { // report progress and time elapsed: chunkProgress(offset, l, (new Date()).getTime() - started); @@ -419,6 +429,8 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({ delete this._gridUnclustered; } + ++this._clearLayersCallCount; + if (this._noanimationUnspiderfy) { this._noanimationUnspiderfy(); }