Animate a point along a route
This source code of this example is adapted from the MapLibre GL JS example - Animate a point along a route.
Uncomment the following line to install leafmap if needed.
In [1]:
Copied!
# %pip install "leafmap[maplibre]"
# %pip install "leafmap[maplibre]"
In [2]:
Copied!
import time
import requests
import leafmap.maplibregl as leafmap
import time
import requests
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=[-100, 40], zoom=3, style="streets")
url = "https://github.com/opengeos/datasets/releases/download/us/arc_with_bearings.geojson"
geojson = requests.get(url).json()
bearings = geojson["features"][0]["properties"]["bearings"]
coordinates = geojson["features"][0]["geometry"]["coordinates"][:-1]
m.add_geojson(geojson, name="route")
origin = [-122.414, 37.776]
destination = [-77.032, 38.913]
point = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {},
"geometry": {"type": "Point", "coordinates": origin},
}
],
}
source = {"type": "geojson", "data": point}
m.add_source("point", source)
layer = {
"id": "point",
"source": "point",
"type": "symbol",
"layout": {
"icon-image": "airport_15",
"icon-rotate": ["get", "bearing"],
"icon-rotation-alignment": "map",
"icon-overlap": "always",
"icon-ignore-placement": True,
},
}
m.add_layer(layer)
m
m = leafmap.Map(center=[-100, 40], zoom=3, style="streets")
url = "https://github.com/opengeos/datasets/releases/download/us/arc_with_bearings.geojson"
geojson = requests.get(url).json()
bearings = geojson["features"][0]["properties"]["bearings"]
coordinates = geojson["features"][0]["geometry"]["coordinates"][:-1]
m.add_geojson(geojson, name="route")
origin = [-122.414, 37.776]
destination = [-77.032, 38.913]
point = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {},
"geometry": {"type": "Point", "coordinates": origin},
}
],
}
source = {"type": "geojson", "data": point}
m.add_source("point", source)
layer = {
"id": "point",
"source": "point",
"type": "symbol",
"layout": {
"icon-image": "airport_15",
"icon-rotate": ["get", "bearing"],
"icon-rotation-alignment": "map",
"icon-overlap": "always",
"icon-ignore-placement": True,
},
}
m.add_layer(layer)
m
Failed to retrieve the MapTiler style. Defaulting to OpenFreeMap 'liberty' style.
In [5]:
Copied!
for index, coordinate in enumerate(coordinates):
point["features"][0]["geometry"]["coordinates"] = coordinate
point["features"][0]["properties"]["bearing"] = bearings[index]
m.set_data("point", point)
time.sleep(0.05)
for index, coordinate in enumerate(coordinates):
point["features"][0]["geometry"]["coordinates"] = coordinate
point["features"][0]["properties"]["bearing"] = bearings[index]
m.set_data("point", point)
time.sleep(0.05)