Control layer visibility using the built-in layer control
Uncomment the following line to install leafmap if needed.
In [1]:
Copied!
# %pip install "leafmap[maplibre]"
# %pip install "leafmap[maplibre]"
In [2]:
Copied!
import leafmap.maplibregl as leafmap
import leafmap.maplibregl as leafmap
In [3]:
Copied!
# import os
# os.environ["MAPTILER_KEY"] = "YOUR_API_KEY"
# import os
# os.environ["MAPTILER_KEY"] = "YOUR_API_KEY"
In [4]:
Copied!
m = leafmap.Map(
center=[-122.19861, 46.21168], zoom=13, pitch=60, bearing=150, style="3d-terrain"
)
m.add_layer_control(bg_layers=True, position="top-left")
m
m = leafmap.Map(
center=[-122.19861, 46.21168], zoom=13, pitch=60, bearing=150, style="3d-terrain"
)
m.add_layer_control(bg_layers=True, position="top-left")
m
An API key is required to use the 3D terrain feature.
In [5]:
Copied!
m = leafmap.Map(
center=[-123.13, 49.254], zoom=11, style="dark-matter", pitch=45, bearing=0
)
url = "https://raw.githubusercontent.com/visgl/deck.gl-data/master/examples/geojson/vancouver-blocks.json"
paint_line = {
"line-color": "white",
"line-width": 2,
}
paint_fill = {
"fill-extrusion-color": {
"property": "valuePerSqm",
"stops": [
[0, "grey"],
[1000, "yellow"],
[5000, "orange"],
[10000, "darkred"],
[50000, "lightblue"],
],
},
"fill-extrusion-height": ["*", 10, ["sqrt", ["get", "valuePerSqm"]]],
"fill-extrusion-opacity": 0.9,
}
m.add_geojson(url, layer_type="line", paint=paint_line, name="blocks-line")
m.add_geojson(url, layer_type="fill-extrusion", paint=paint_fill, name="blocks-fill")
m.add_layer_control(layer_ids=["blocks-line", "blocks-fill"])
m
m = leafmap.Map(
center=[-123.13, 49.254], zoom=11, style="dark-matter", pitch=45, bearing=0
)
url = "https://raw.githubusercontent.com/visgl/deck.gl-data/master/examples/geojson/vancouver-blocks.json"
paint_line = {
"line-color": "white",
"line-width": 2,
}
paint_fill = {
"fill-extrusion-color": {
"property": "valuePerSqm",
"stops": [
[0, "grey"],
[1000, "yellow"],
[5000, "orange"],
[10000, "darkred"],
[50000, "lightblue"],
],
},
"fill-extrusion-height": ["*", 10, ["sqrt", ["get", "valuePerSqm"]]],
"fill-extrusion-opacity": 0.9,
}
m.add_geojson(url, layer_type="line", paint=paint_line, name="blocks-line")
m.add_geojson(url, layer_type="fill-extrusion", paint=paint_fill, name="blocks-fill")
m.add_layer_control(layer_ids=["blocks-line", "blocks-fill"])
m
In [6]:
Copied!
m.layer_interact()
m.layer_interact()
In [7]:
Copied!
m = leafmap.Map(center=[4.4562, 51.9066], zoom=14)
m.add_basemap("Esri.WorldImagery")
url = "https://storage.googleapis.com/ahp-research/overture/pmtiles/overture.pmtiles"
style = {
"version": 8,
"sources": {
"example_source": {
"type": "vector",
"url": "pmtiles://" + url,
"attribution": "PMTiles",
}
},
"layers": [
{
"id": "buildings",
"source": "example_source",
"source-layer": "buildings",
"type": "fill",
"paint": {"fill-color": "#FFFFB3", "fill-opacity": 0.5},
},
{
"id": "places",
"source": "example_source",
"source-layer": "places",
"type": "fill",
"paint": {"fill-color": "#BEBADA", "fill-opacity": 0.5},
},
{
"id": "roads",
"source": "example_source",
"source-layer": "roads",
"type": "line",
"paint": {"line-color": "#FB8072"},
},
],
}
m.add_pmtiles(
url,
style=style,
visible=True,
opacity=1.0,
tooltip=True,
)
m.set_zoom(14)
m.add_layer_control()
m
m = leafmap.Map(center=[4.4562, 51.9066], zoom=14)
m.add_basemap("Esri.WorldImagery")
url = "https://storage.googleapis.com/ahp-research/overture/pmtiles/overture.pmtiles"
style = {
"version": 8,
"sources": {
"example_source": {
"type": "vector",
"url": "pmtiles://" + url,
"attribution": "PMTiles",
}
},
"layers": [
{
"id": "buildings",
"source": "example_source",
"source-layer": "buildings",
"type": "fill",
"paint": {"fill-color": "#FFFFB3", "fill-opacity": 0.5},
},
{
"id": "places",
"source": "example_source",
"source-layer": "places",
"type": "fill",
"paint": {"fill-color": "#BEBADA", "fill-opacity": 0.5},
},
{
"id": "roads",
"source": "example_source",
"source-layer": "roads",
"type": "line",
"paint": {"line-color": "#FB8072"},
},
],
}
m.add_pmtiles(
url,
style=style,
visible=True,
opacity=1.0,
tooltip=True,
)
m.set_zoom(14)
m.add_layer_control()
m
In [8]:
Copied!
m.layer_interact()
m.layer_interact()