Add live realtime data
Use realtime GeoJSON data streams to move a symbol on your map.
This source code of this example is adapted from the MapLibre GL JS example - Add live realtime data.
Uncomment the following line to install leafmap if needed.
In [1]:
Copied!
# %pip install "leafmap[maplibre]"
# %pip install "leafmap[maplibre]"
In [2]:
Copied!
import requests
import time
import leafmap.maplibregl as leafmap
import requests
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!
m = leafmap.Map(center=[0, 20], zoom=1, style="streets")
source = {
"type": "geojson",
"data": {"type": "Feature", "geometry": {"type": "Point", "coordinates": [0, 0]}},
}
m.add_source("drone", source)
layer = {
"id": "drone",
"type": "symbol",
"source": "drone",
"layout": {"icon-image": "rocket_15"},
}
m.add_layer(layer)
m
m = leafmap.Map(center=[0, 20], zoom=1, style="streets")
source = {
"type": "geojson",
"data": {"type": "Feature", "geometry": {"type": "Point", "coordinates": [0, 0]}},
}
m.add_source("drone", source)
layer = {
"id": "drone",
"type": "symbol",
"source": "drone",
"layout": {"icon-image": "rocket_15"},
}
m.add_layer(layer)
m
Failed to retrieve the MapTiler style. Defaulting to OpenFreeMap 'liberty' style.
In [5]:
Copied!
def update_location(num_times, time_interval):
url = "https://www.random.org/decimal-fractions/?num=2&dec=10&col=1&format=plain&rnd=new"
for _ in range(num_times):
response = requests.get(url)
data = response.text
data = data.strip().split("\n")
# Takes the two random numbers between 0 and 1 and converts them to degrees
lat, lon = float(data[0]) * 180 - 90, float(data[1]) * 180 - 90
geojson = {
"type": "Feature",
"geometry": {"type": "Point", "coordinates": [lon, lat]},
}
m.set_data("drone", geojson)
time.sleep(time_interval)
def update_location(num_times, time_interval):
url = "https://www.random.org/decimal-fractions/?num=2&dec=10&col=1&format=plain&rnd=new"
for _ in range(num_times):
response = requests.get(url)
data = response.text
data = data.strip().split("\n")
# Takes the two random numbers between 0 and 1 and converts them to degrees
lat, lon = float(data[0]) * 180 - 90, float(data[1]) * 180 - 90
geojson = {
"type": "Feature",
"geometry": {"type": "Point", "coordinates": [lon, lat]},
}
m.set_data("drone", geojson)
time.sleep(time_interval)
In [6]:
Copied!
update_location(20, 0.5)
update_location(20, 0.5)