[GIS] Leaflet API – select one overlay at a time like base layers

leaflet

I have a set of basemaps and overlays on my simple web mapping application with Leaflet. I would like to be able to select one overlay at a time like base layers so that multiple selection of overlays is not allowed.

I cannot make those overlays into base layers as at least one base layer should be visible on the map at any time.

Currently I have the following code snippet.

var baseLayers = {baselayer1, baselayer2, baselayer3};
var overlayMaps = {overlay1, overlay2, overlay3};
L.control.layers(baseLayers, overlayMaps).addTo(map);

Best Answer

Create a second L.Control.Layers, with your "overlays" as "base layers".

var baseLayers = {baselayer1, baselayer2, baselayer3};
var overlayMaps = {overlay1, overlay2, overlay3};
L.control.layers(baseLayers).addTo(map);
L.control.layers(overlayMaps).addTo(map);

Keep in mind that "overlay" and "base layer" is just nomenclature for L.Control.Layers ("base layer"s are exclusive among themselves in the context of a layers control, "overlay"s are not). Those words have no meaning in the context of a L.Map, which means that if you don't have a layers control, then you cannot tell a layer is a base layer or an overlay.