Variable label placement with offset
This source code of this example is adapted from the MapLibre GL JS example - Variable label placement with offset.
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=[-77.04, 38.907], zoom=11, style="streets")
places = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {"description": "Ford's Theater", "icon": "theatre"},
"geometry": {"type": "Point", "coordinates": [-77.038659, 38.931567]},
},
{
"type": "Feature",
"properties": {"description": "The Gaslight", "icon": "theatre"},
"geometry": {"type": "Point", "coordinates": [-77.003168, 38.894651]},
},
{
"type": "Feature",
"properties": {"description": "Horrible Harry's", "icon": "bar"},
"geometry": {"type": "Point", "coordinates": [-77.090372, 38.881189]},
},
{
"type": "Feature",
"properties": {"description": "Bike Party", "icon": "bicycle"},
"geometry": {"type": "Point", "coordinates": [-77.052477, 38.943951]},
},
{
"type": "Feature",
"properties": {"description": "Rockabilly Rockstars", "icon": "music"},
"geometry": {"type": "Point", "coordinates": [-77.031706, 38.914581]},
},
{
"type": "Feature",
"properties": {"description": "District Drum Tribe", "icon": "music"},
"geometry": {"type": "Point", "coordinates": [-77.020945, 38.878241]},
},
{
"type": "Feature",
"properties": {"description": "Motown Memories", "icon": "music"},
"geometry": {"type": "Point", "coordinates": [-77.007481, 38.876516]},
},
],
}
source = {"type": "geojson", "data": places}
m.add_source("places", source)
layer = {
"id": "poi-labels",
"type": "symbol",
"source": "places",
"layout": {
"text-field": ["get", "description"],
"text-variable-anchor-offset": [
"top",
[0, 1],
"bottom",
[0, -2],
"left",
[1, 0],
"right",
[-2, 0],
],
"text-justify": "auto",
"icon-image": ["concat", ["get", "icon"], "_15"],
},
}
m.add_layer(layer)
m
m = leafmap.Map(center=[-77.04, 38.907], zoom=11, style="streets")
places = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {"description": "Ford's Theater", "icon": "theatre"},
"geometry": {"type": "Point", "coordinates": [-77.038659, 38.931567]},
},
{
"type": "Feature",
"properties": {"description": "The Gaslight", "icon": "theatre"},
"geometry": {"type": "Point", "coordinates": [-77.003168, 38.894651]},
},
{
"type": "Feature",
"properties": {"description": "Horrible Harry's", "icon": "bar"},
"geometry": {"type": "Point", "coordinates": [-77.090372, 38.881189]},
},
{
"type": "Feature",
"properties": {"description": "Bike Party", "icon": "bicycle"},
"geometry": {"type": "Point", "coordinates": [-77.052477, 38.943951]},
},
{
"type": "Feature",
"properties": {"description": "Rockabilly Rockstars", "icon": "music"},
"geometry": {"type": "Point", "coordinates": [-77.031706, 38.914581]},
},
{
"type": "Feature",
"properties": {"description": "District Drum Tribe", "icon": "music"},
"geometry": {"type": "Point", "coordinates": [-77.020945, 38.878241]},
},
{
"type": "Feature",
"properties": {"description": "Motown Memories", "icon": "music"},
"geometry": {"type": "Point", "coordinates": [-77.007481, 38.876516]},
},
],
}
source = {"type": "geojson", "data": places}
m.add_source("places", source)
layer = {
"id": "poi-labels",
"type": "symbol",
"source": "places",
"layout": {
"text-field": ["get", "description"],
"text-variable-anchor-offset": [
"top",
[0, 1],
"bottom",
[0, -2],
"left",
[1, 0],
"right",
[-2, 0],
],
"text-justify": "auto",
"icon-image": ["concat", ["get", "icon"], "_15"],
},
}
m.add_layer(layer)
m
Failed to retrieve the MapTiler style. Defaulting to OpenFreeMap 'liberty' style.
In [5]:
Copied!
m.rotate_to(bearing=180, options={"duration": 10000})
m.rotate_to(bearing=180, options={"duration": 10000})