Skip to content

Commit

Permalink
Add addLayers, takes an array of markers and adds them in bulk for aw…
Browse files Browse the repository at this point in the history
…esome performance. TODO: Docs+Example. Refs Leaflet#59
  • Loading branch information
danzel committed Oct 10, 2012
1 parent 5d31d7c commit 9dab59f
Showing 1 changed file with 27 additions and 2 deletions.
29 changes: 27 additions & 2 deletions src/MarkerClusterGroup.js
Expand Up @@ -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) {
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 9dab59f

Please sign in to comment.