Leaflet

an open-source JavaScript library
for mobile-friendly interactive maps

← Tutorials

Layer Groups and Layers Control

This tutorial will show you how to group several layers into one, and how to use the layers control to allow users to easily switch different layers on your map.

See this example stand-alone.

Layer Groups

Let’s suppose you have a bunch of layers you want to combine into a group to handle them as one in your code:

var littleton = L.marker([39.61, -105.02]).bindPopup('This is Littleton, CO.'),
	denver    = L.marker([39.74, -104.99]).bindPopup('This is Denver, CO.'),
	aurora    = L.marker([39.73, -104.8]).bindPopup('This is Aurora, CO.'),
    golden    = L.marker([39.77, -105.23]).bindPopup('This is Golden, CO.');

Instead of adding them directly to the map, you can do the following, using the LayerGroup class:

var cities = L.layerGroup([littleton, denver, aurora, golden]);

Easy enough! Now you have a cities layer that combines your city markers into one layer you can add or remove from the map at once.

Layers Control

Leaflet has a nice little control that allows your users to control which layers they see on your map. In addition to showing you how to use it, we’ll also show you another handy use for layer groups.

There are two types of layers: (1) base layers that are mutually exclusive (only one can be visible on your map at a time), e.g. tile layers, and (2) overlays, which are all the other stuff you put over the base layers. In this example, we want to have two base layers (OpenStreetMap ‘osm’ and OpenStreetMap.Hot osmHOT base map) to switch between, and an overlay to switch on and off: the city markers we created earlier.

Now let’s create those base layers and add the default ones to the map:

var osm = L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', {
	maxZoom: 19,
	attribution: '© OpenStreetMap'
});

var osmHOT = L.tileLayer('https://{s}.tile.openstreetmap.fr/hot/{z}/{x}/{y}.png', {
	maxZoom: 19,
	attribution: '© OpenStreetMap contributors, Tiles style by Humanitarian OpenStreetMap Team hosted by OpenStreetMap France'});

var map = L.map('map', {
	center: [39.73, -104.99],
	zoom: 10,
	layers: [osm, cities]
});

Next, we’ll create two objects. One will contain our base layers and one will contain our overlays. These are just simple objects with key/value pairs. The key sets the text for the layer in the control (e.g. “OpenStreetMap”), while the corresponding value is a reference to the layer (e.g. osm).

var baseMaps = {
	"OpenStreetMap": osm,
	"OpenStreetMap.HOT": osmHOT
};

var overlayMaps = {
	"Cities": cities
};

Now, all that’s left to do is to create a Layers Control and add it to the map. The first argument passed when creating the layers control is the base layers object. The second argument is the overlays object. Both arguments are optional: you can pass just a base layers object by omitting the second argument, or just an overlays objects by passing null as the first argument. In each case, the omitted layer type will not appear for the user to select.

var layerControl = L.control.layers(baseMaps, overlayMaps).addTo(map);

Note that we added osm and cities layers to the map but didn’t add osmHOT. The layers control is smart enough to detect what layers we’ve already added and have corresponding checkboxes and radioboxes set.

Also note that when using multiple base layers, only one of them should be added to the map at instantiation, but all of them should be present in the base layers object when creating the layers control.

You can also style the keys when you define the objects for the layers. For example, this code will make the label for the OpenStreetMap.HOT map red:

var baseMaps = {
	"OpenStreetMap": osm,
	"<span style='color: red'>OpenStreetMap.HOT</span>": osmHOT
};

Finally, you can add or remove base layers or overlays dynamically:

var crownHill = L.marker([39.75, -105.09]).bindPopup('This is Crown Hill Park.'),
    rubyHill = L.marker([39.68, -105.00]).bindPopup('This is Ruby Hill Park.');
    
var parks = L.layerGroup([crownHill, rubyHill]);
var openTopoMap = L.tileLayer('https://{s}.tile.opentopomap.org/{z}/{x}/{y}.png', {
	maxZoom: 19,
	attribution: 'Map data: © OpenStreetMap contributors, SRTM | Map style: © OpenTopoMap (CC-BY-SA)'
});

layerControl.addBaseLayer(openTopoMap, "OpenTopoMap");
layerControl.addOverlay(parks, "Parks");

Now let’s view the result on a separate page →