Animate point along line

Animate a point
This source code of this example is adapted from the MapLibre GL JS example - Animate a point.
Uncomment the following line to install leafmap if needed.
| # %pip install "leafmap[maplibre]"
|
| import math
import time
import leafmap.maplibregl as leafmap
|
To run this notebook, you will need an API key from MapTiler. Once you have the API key, you can uncomment the following code block and replace YOUR_API_KEY with your actual API key. Then, run the code block code to set the API key as an environment variable.
| # import os
# os.environ["MAPTILER_KEY"] = "YOUR_API_KEY"
|
| def point_on_circle(angle, radius):
return {
"type": "Point",
"coordinates": [math.cos(angle) * radius, math.sin(angle) * radius],
}
|
1
2
3
4
5
6
7
8
9
10
11
12 | m = leafmap.Map(center=[0, 0], zoom=2, style="streets")
radius = 20
source = {"type": "geojson", "data": point_on_circle(0, radius)}
m.add_source("point", source)
layer = {
"id": "point",
"source": "point",
"type": "circle",
"paint": {"circle-radius": 10, "circle-color": "#007cbf"},
}
m.add_layer(layer)
m
|
| def animate_marker(duration, frame_rate, radius):
start_time = time.time()
while (time.time() - start_time) < duration:
timestamp = (time.time() - start_time) * 1000 # Convert to milliseconds
angle = timestamp / 1000 # Divisor controls the animation speed
point = point_on_circle(angle, radius)
m.set_data("point", point)
# Wait for the next frame
time.sleep(1 / frame_rate)
|
| duration = 10 # Duration of the animation in seconds
frame_rate = 30 # Frames per second
|
| animate_marker(duration, frame_rate, radius)
|
