← Back to the list of examples
This guide 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.
Note: this only works in the latest (master) version of Leaflet.
View example on a separate page →
Lets suppose you have a bunch of layers you want to combine into a group to handle them as one in your code:
var littletonMarker = new L.Marker(new L.LatLng(39.61, -105.02)).bindPopup("This is Littleton, CO."),
denverMarker = new L.Marker(new L.LatLng(39.74, -104.99)).bindPopup("This is Denver, CO."),
auroraMarker = new L.Marker(new L.LatLng(39.73, -104.8)).bindPopup("This is Aurora, CO."),
goldenMarker = new L.Marker(new L.LatLng(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 citiesLayer = new L.LayerGroup();
citiesLayer.addLayer(littletonMarker)
.addLayer(denverMarker)
.addLayer(auroraMarker)
.addLayer(goldenMarker);
map.addLayer(citiesLayer);
Easy enough! Now you have citiesLayer
that combines your city markers into one layer you can add or remove from the map at once.
Leaflet has a nice little control that allows your users control what layers they want to see on your map. In addition to showing you how to use it, we'll show another handy use for layer groups.
There are two types of layers — base layers that are mutually exclusive (only one can be visible on your map), e.g. tile layers, and overlays — all the other stuff you put over the base layers. In this example, we want to have two base layers (minimal and night-style base map) to switch between, and two overlays to switch on and off — a pink motorways overlay and city markers (those we created earlier). Lets create those layers and add the default ones to the map:
var cloudmadeUrl = 'http://{s}.tile.cloudmade.com/YOUR-API-KEY/{styleId}/256/{z}/{x}/{y}.png',
cloudmadeAttribution = 'Map data © 2011 OpenStreetMap contributors, Imagery © 2011 CloudMade',
cloudmadeOptions = {maxZoom: 18, attribution: cloudmadeAttribution};
var minimal = new L.TileLayer(cloudmadeUrl, cloudmadeOptions, {styleId: 22677}),
midnightCommander = new L.TileLayer(cloudmadeUrl, cloudmadeOptions, {styleId: 999}),
motorways = new L.TileLayer(cloudmadeUrl, cloudmadeOptions, {styleId: 46561});
var map = new L.Map('map', {
center: new L.LatLng(39.73, -104.99),
zoom: 10,
layers: [minimal, motorways, citiesLayer]
});
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 is what sets the text for the layer in the control (e.g. "Night View"). The corresponding value is a reference to the layer (e.g. midnightCommander
).
var baseMaps = {
"Minimal": minimal,
"Night View": midnightCommander
};
var overlayMaps = {
"Motorways": motorways,
"Cities": citiesLayer
};
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 — for example, you can pass just a base layers object by ommiting the second argument, or just an overlays objects by passing null
as the first argument.
var layersControl = new L.Control.Layers(baseMaps, overlayMaps);
map.addControl(layersControl);
Now lets view the result on a separate page →
© 2011 CloudMade. Map data © 2011 OpenStreetMap contributors, CC-BY-SA.