Skip to content

Add labels

image image image

Add labels to the map

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
m = leafmap.Map(style="positron", projection="globe")
m.add_basemap("Satellite")

geojson = "https://github.com/opengeos/datasets/releases/download/world/mgrs_grid_zone.geojson"
m.add_geojson(geojson)
m.add_labels(geojson, "GZD", text_color="white", min_zoom=2, max_zoom=10)
m

image

1
2
3
4
5
6
7
8
m = leafmap.Map(style="positron", projection="globe")
m.add_basemap("Satellite")

geojson = "https://github.com/opengeos/datasets/releases/download/world/mgrs_grid_zone.geojson"
paint = {"line-color": "red"}
m.add_geojson(geojson, layer_type="line", paint=paint)
m.add_labels(geojson, "GZD", text_color="white", min_zoom=2)
m

image

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
m = leafmap.Map(style="positron", projection="globe")
m.add_basemap("Satellite")

geojson = "https://github.com/opengeos/datasets/releases/download/vector/cables.geojson"
paint = {"line-color": "red"}
m.add_geojson(geojson, layer_type="line", paint=paint)

layout = {
    "visibility": "visible",
    "text-field": ["get", "name"],
    "text-size": 14,
    "text-anchor": "center",
    "symbol-placement": "line",
    "text-rotation-alignment": "map",
    "text-offset": [0, -0.6],
    "symbol-avoid-edges": False,
    "text-font": ["Noto Sans Regular"],
}

paint = {
    "text-color": "red",
    "text-halo-color": "white",
    "text-halo-width": 2,
}

m.add_labels(geojson, "id", text_color="white", min_zoom=2, layout=layout, paint=paint)
m