Skip to content

Layer groups

image image image

Create layer group toggle buttons

This example shows how to create layer group toggle buttons for the layer manager.

Uncomment the following line to install leafmap if needed.

1
# %pip install "leafmap[maplibre]"
1
import leafmap.maplibregl as leafmap
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
m = leafmap.Map()

## Add basemap layers
m.add_basemap("USGS.Imagery")
m.add_basemap("OpenTopoMap")

# Add GeoJSON layers
point_url = (
    "https://github.com/opengeos/datasets/releases/download/world/world_cities.geojson"
)
m.add_geojson(point_url, name="Points")
line_url = (
    "https://github.com/opengeos/datasets/releases/download/vector/cables.geojson"
)
m.add_geojson(line_url, name="Lines")

# Create layer groups
groups = {"Basemap": ["USGS.Imagery", "OpenTopoMap"], "GeoJSON": ["Points", "Lines"]}

# Add a layer manager with group toggle buttons
m.add_layer_manager(label="Layer Manager", groups=groups)

# Display the map
m

image