Your first MapLibreum map¶
Build a polished, shareable walking-tour map in a few minutes. Along the way you will learn the four ideas used throughout MapLibreum: create a map, add objects, add controls, and export HTML.
You will use: Map, markers, popups, tooltips, a line, automatic bounds, controls, and save. No API key or downloaded dataset is required.
from pathlib import Path
from tempfile import gettempdir
from maplibreum import Map, Tooltip, __version__
from maplibreum.core import PolyLine
print(f"MapLibreum {__version__}")
MapLibreum 0.2.3
Tell one small geographic story¶
This example connects three landmarks in Hiroshima. Coordinates everywhere in MapLibreum use MapLibre's [longitude, latitude] order. The popup accepts HTML; the tooltip is the short label shown on hover.
stops = [
{
"name": "Peace Memorial Park",
"coordinates": [132.4523, 34.3927],
"description": "A reflective riverside landscape at the heart of the city.",
"color": "#e63946",
},
{
"name": "Hiroshima Castle",
"coordinates": [132.4594, 34.4028],
"description": "A reconstructed castle surrounded by a moat and park.",
"color": "#457b9d",
},
{
"name": "Shukkeien Garden",
"coordinates": [132.4674, 34.3993],
"description": "A compact landscape garden whose name means “shrunken scenery.”",
"color": "#2a9d8f",
},
]
tour = Map(title="A short walk through Hiroshima", height="560px")
for stop in stops:
tour.add_marker(
coordinates=stop["coordinates"],
color=stop["color"],
tooltip=stop["name"],
popup=(
f"<h3 style='margin:0 0 6px'>{stop['name']}</h3>"
f"<p style='margin:0'>{stop['description']}</p>"
),
)
PolyLine(
locations=[stop["coordinates"] for stop in stops],
color="#f4a261",
weight=5,
tooltip=Tooltip("Suggested walking sequence"),
).add_to(tour)
tour.fit_bounds([[132.448, 34.388], [132.471, 34.406]], padding=45)
tour.add_control(
"navigation",
position="top-right",
options={"showZoom": True, "showCompass": True},
)
tour.add_control("fullscreen", position="top-right")
tour.add_control("scale", position="bottom-left")
tour
Export a standalone page¶
A MapLibreum map is ordinary HTML. Save it, attach it to an email, publish it on static hosting, or embed it in a larger application. This cell uses your temporary directory so running the notebook never dirties the repository.
output = Path(gettempdir()) / "maplibreum-hiroshima-tour.html"
tour.save(output)
print(f"Saved {output.stat().st_size:,} bytes to {output}")
Saved 21,091 bytes to /tmp/maplibreum-hiroshima-tour.html
What to remember¶
- Start with
Map(...). - Add objects with
map.add_*orobject.add_to(map). - Return the map as the final expression to display it in Jupyter.
- Call
save(...)when the map is ready to travel beyond the notebook.
Next: build a data-driven map whose color and width come directly from feature properties.