admin管理员组

文章数量:1242825

I'm using leaflet-groupedlayercontrol to add my grouped layers to the map and I have a problem:

All the layers are deselected, but I want to select all by default. I am using checkboxes, not radio buttons.

var groupedOverlays = {
    "Select": {}
};

groupedOverlays["Select"]["Group 1"] = groups[0];
groupedOverlays["Select"]["Group 2"] = groups[1];
groupedOverlays["Select"]["Group 3"] = groups[2];

// Use the custom grouped layer control, not "L.control.layers"
L.control.groupedLayers(null, groupedOverlays, {collapsed:false}).addTo(map);

I tried to select them with JS, but didn't worked.

If you know a solution for LeafletJS, but not for that particular plugin, it's ok too.

I'm using leaflet-groupedlayercontrol to add my grouped layers to the map and I have a problem:

All the layers are deselected, but I want to select all by default. I am using checkboxes, not radio buttons.

var groupedOverlays = {
    "Select": {}
};

groupedOverlays["Select"]["Group 1"] = groups[0];
groupedOverlays["Select"]["Group 2"] = groups[1];
groupedOverlays["Select"]["Group 3"] = groups[2];

// Use the custom grouped layer control, not "L.control.layers"
L.control.groupedLayers(null, groupedOverlays, {collapsed:false}).addTo(map);

I tried to select them with JS, but didn't worked.

If you know a solution for LeafletJS, but not for that particular plugin, it's ok too.

Share Improve this question asked Mar 30, 2017 at 8:48 BacchusBacchus 5159 silver badges22 bronze badges 1
  • I am using leafletjs./examples/layers-control ... is the same thing or need other question? – Peter Krauss Commented Dec 21, 2018 at 10:08
Add a ment  | 

2 Answers 2

Reset to default 14

Whether a Leaflet layer is ""selected"" or not in the built-in L.Control.Layers depends on whether the layer is added to the map or not.

e.g. this will display a L.Control.Layers with the checkbox off:

L.control.layers({}, { 
    Foo: L.marker([0,0]) 
}).addTo(map)

...while this will display it with the checkbox on:

L.control.layers({}, { 
    Foo: L.marker([0,0]).addTo(map)
}).addTo(map)

I expect the behaviour of the GroupedLayers control to be similar. Just double-check whether you add the layers to the map or not. Also notice that the checkboxes' state updates whenever layers are added/removed to/from the map by any means, at any time.

Defining map set layers (it means default)

const map = L.map('map', {
        crs: L.CRS.Simple,
        minZoom: -2,
        maxZoom: 0,
        ZoomLevel: 1,
        layers: [outpost,inter,stand]
    });

ourpost, inter, stand layers will be selected

本文标签: javascriptLeafletJSDefault selection of layersStack Overflow