Esri Leaflet Layers – Ensuring Esri Leaflet Layers Are Not Turned Off by Default in Map Layer Control Box

esri-leafletlayers

I have a series of esri leaflet layers in a variable called "baseLayers" but want to initiate the map with all of them turned off by default inside the layer control box. I've tried playing with the L.control.layers method options but can't find anything that works. What I have currently is this:

L.control.layers(null, baseLayers, {collapsed:false}).addTo(map);

The 'addTo(map)' command is what I think causes all layers to show up as enabled in the layer control box by default, but if I remove that part of the code then the layers just don't show up at all in the control box. Any ideas?

Best Answer

have you already seen this helpful tutorial?

http://leafletjs.com/examples/layers-control/

because the control expects a object composed of basemaps (that can only be displayed one at a time) to be passed to the constructor in the first position and an object composed of overlays that can be checked on and off individually to be passed in the second position, the variable name you've chosen a bit confusing.

The 'addTo(map)' command is what I think causes all layers to show up as enabled in the layer control box by default

the layers control addTo() method doesn't have any control over individual layer visibility, it just ensures that the control itself is displayed on top of the map.

using the live sample referenced in the tutorial above as an example, the reason the cities LayerGroup is displayed by default is because the layer was added to the map back when it was constructed.

/* 
 if you remove 'cities' from the array below
 the layer will not be drawn on the map 
 and its checkbox will be 'unchecked'  
*/
var map = L.map('map', {
    center: [39.73, -104.99],
    zoom: 10,
    layers: [grayscale, cities]
});
Related Question