diff --git a/debug/control/control-layers.html b/debug/control/control-layers.html index 0b1ea3a3..4f8d34a6 100644 --- a/debug/control/control-layers.html +++ b/debug/control/control-layers.html @@ -26,15 +26,21 @@ var map = new L.Map('map').addLayer(cloudmade).setView(new L.LatLng(50.5, 30.51), 15); - var marker = new L.Marker(map.getCenter()); + var marker = new L.Marker(new L.LatLng(50.5, 30.505)); map.addLayer(marker); - var layersControl = new L.Control.Layers(); - map.addControl(layersControl); + var marker2 = new L.Marker(new L.LatLng(50.502, 30.515)); + map.addLayer(marker2); - layersControl.addBaseLayer(cloudmade, 'CloudMade Fresh'); - layersControl.addBaseLayer(cloudmade2, 'CloudMade Pale Dawn'); - layersControl.addOverlay(marker, 'Some marker'); + var layersControl = new L.Control.Layers({ + 'CloudMade Fresh': cloudmade, + 'CloudMade Pale Dawn': cloudmade2, + }, { + 'Some marker': marker, + 'Another marker': marker2 + }); + + map.addControl(layersControl); diff --git a/src/control/Control.Layers.js b/src/control/Control.Layers.js index 3296c53b..09bd377f 100644 --- a/src/control/Control.Layers.js +++ b/src/control/Control.Layers.js @@ -4,10 +4,22 @@ L.Control.Layers = L.Class.extend({ collapsed: true }, - initialize: function(options) { + initialize: function(baseLayers, overlays, options) { L.Util.setOptions(this, options); this._layers = {}; + + for (var i in baseLayers) { + if (baseLayers.hasOwnProperty(i)) { + this._addLayer(baseLayers[i], i); + } + } + + for (i in overlays) { + if (overlays.hasOwnProperty(i)) { + this._addLayer(overlays[i], i, true); + } + } }, onAdd: function(map) { @@ -28,17 +40,20 @@ L.Control.Layers = L.Class.extend({ addBaseLayer: function(layer, name) { this._addLayer(layer, name); this._update(); + return this; }, addOverlay: function(layer, name) { this._addLayer(layer, name, true); this._update(); + return this; }, removeLayer: function(layer) { var id = L.Util.stamp(layer); delete this._layers[id]; this._update(); + return this; }, _initLayout: function() {