From 9dab59f1754a8fd82164ba958d4bd7fd1792bf3d Mon Sep 17 00:00:00 2001 From: danzel Date: Wed, 10 Oct 2012 17:18:03 +1300 Subject: [PATCH] Add addLayers, takes an array of markers and adds them in bulk for awesome performance. TODO: Docs+Example. Refs #59 --- src/MarkerClusterGroup.js | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/src/MarkerClusterGroup.js b/src/MarkerClusterGroup.js index 1dba89c10..92a387286 100644 --- a/src/MarkerClusterGroup.js +++ b/src/MarkerClusterGroup.js @@ -43,12 +43,13 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({ addLayer: function (layer) { if (layer instanceof L.LayerGroup) { + var array = []; for (var i in layer._layers) { if (layer._layers.hasOwnProperty(i)) { - this.addLayer(layer._layers[i]); + array.push(layer._layers[i]); } } - return this; + return this.addLayers(array); } if (this.options.singleMarkerMode) { @@ -124,6 +125,30 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({ return this; }, + addLayers: function (layersArray) { + if (!this._map) { + this._needsClustering = this._needsClustering.concat(layersArray); + return this; + } + + for (var i = 0, l = layersArray.length; i < l; i++) { + var m = layersArray[i]; + this._addLayer(m, this._maxZoom); + + //If we just made a cluster of size 2 then we need to remove the other marker from the map (if it is) or we never will + if (m.__parent) { + if (m.__parent.getChildCount() === 2) { + var markers = m.__parent.getAllChildMarkers(), + otherMarker = markers[0] === m ? markers[1] : markers[0]; + L.FeatureGroup.prototype.removeLayer.call(this, otherMarker); + } + } + } + this._topClusterLevel._recursivelyAddChildrenToMap(null, this._zoom, this._currentShownBounds); + + return this; + }, + clearLayers: function () { //Need our own special implementation as the LayerGroup one doesn't work for us