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.
In [1]:
Copied!
# %pip install "leafmap[maplibre]"
# %pip install "leafmap[maplibre]"
In [2]:
Copied!
import math
import time
import leafmap.maplibregl as leafmap
import math
import time
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!
def point_on_circle(angle, radius):
return {
"type": "Point",
"coordinates": [math.cos(angle) * radius, math.sin(angle) * radius],
}
def point_on_circle(angle, radius):
return {
"type": "Point",
"coordinates": [math.cos(angle) * radius, math.sin(angle) * radius],
}
In [5]:
Copied!
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
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
Failed to retrieve the MapTiler style. Defaulting to OpenFreeMap 'liberty' style.
In [6]:
Copied!
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)
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)
In [7]:
Copied!
duration = 10 # Duration of the animation in seconds
frame_rate = 30 # Frames per second
duration = 10 # Duration of the animation in seconds
frame_rate = 30 # Frames per second
In [8]:
Copied!
animate_marker(duration, frame_rate, radius)
animate_marker(duration, frame_rate, radius)