# /// script
# requires-python = ">=3.12"
# dependencies = [
# "aiohttp",
# "geopy",
# "lonboard",
# ]
# ///
Imports¶
from geopy.adapters import AioHTTPAdapter
from geopy.geocoders import Nominatim
from lonboard import Map
from lonboard.basemap import CartoStyle, MaplibreBasemap
from lonboard.controls import (
FullscreenControl,
GeocoderControl,
NavigationControl,
ScaleControl,
)
First, we create a new geocoder instance. GeoPy supports many different geocoder providers. Some may require API keys or other authentication. This is your responsibility to provide when creating the geocoder instance.
In our case, we'll use the Nominatim class, which is provided by https://nominatim.org. The Nominatim usage policy requires that you specify a custom user agent that identifies your application. In this case, we'll use the string "lonboard-app".
In order for the Python-browser communication to work well, it's also required that you create a geocoder in async mode. Here we pass in the AioHTTPAdapter, which ensures that geocoding requests can be done asynchronously.
geocoder = Nominatim(user_agent="lonboard-app", adapter_factory=AioHTTPAdapter)
Then, create a new GeocoderControl instance. Since we're using the GeoPy integration, we use the from_geopy constructor.
geocoder_control = GeocoderControl.from_geopy(geocoder)
Next, just include control in the list passed to the controls parameter.
Note that since we're overriding the controls argument, if we want to restore the default controls, such as the FullscreenControl, NavigationControl, and ScaleControl, we'll have to add them.
controls = [geocoder_control, NavigationControl(), ScaleControl(), FullscreenControl()]
m = Map([], basemap=MaplibreBasemap(style=CartoStyle.Voyager), controls=controls)
m
VBox(children=(<lonboard._map.Map object at 0x109cf31a0>, VBox(children=(ErrorOutput(), ErrorOutput(), ErrorOu…