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
# 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],
}
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)