Skip to content

Control.Layers

The layers control gives users the ability to switch between different base layers and switch overlays on/off (check out the detailed example). Extends Control.

Usage example

js
const baseLayers = {
"Mapbox": mapbox,
"OpenStreetMap": osm
};

const overlays = {
"Marker": marker,
"Roads": roadsLayer
};

new Control.Layers(baseLayers, overlays).addTo(map);

The baseLayers and overlays parameters are object literals with layer names as keys and Layer objects as values:

js
{
"<someName1>": layer1,
"<someName2>": layer2
}

The layer names can contain HTML, which allows you to add additional styling to the items:

js
{"<img src='my-layer-icon' /> <span class='my-layer-item'>My Layer</span>": myLayer}

Options

OptionTypeDefaultDescription
collapsedBooleantrueIf true, the control will be collapsed into an icon and expanded on pointer hover, touch, or keyboard activation.
collapseDelayNumber0Collapse delay in milliseconds. If greater than 0, the control will remain open longer, making it easier to scroll through long layer lists.
autoZIndexBooleantrueIf true, the control will assign zIndexes in increasing order to all of its layers so that the order is preserved when switching them on/off.
hideSingleBaseBooleanfalseIf true, the base layers in the control will be hidden when there is only one.
sortLayersBooleanfalseWhether to sort the layers. When false, layers will keep the order
sortFunctionFunction*A compare function

Events

EventDataDescription
baselayerchangeFired when the base layer is changed through the layers control.
overlayaddFired when an overlay is selected through the layers control.
overlayremoveFired when an overlay is deselected through the layers control.