Add live realtime data

Use realtime GeoJSON data streams to move a symbol on your map.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Add live realtime data</title>
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" />
<script src="https://unpkg.com/trackasia-gl@1.0.5/dist/trackasia-gl.js"></script>
<link href="https://unpkg.com/trackasia-gl@1.0.5/dist/trackasia-gl.css" rel="stylesheet" />
<style>
body { margin: 0; padding: 0; }
#map { position: absolute; top: 0; bottom: 0; width: 100%; }
</style>
</head>
<body>
<div id="map"></div>
<script>
var map = new trackasiagl.Map({
container: 'map',
style: 'https://maps.track-asia.com/styles/v1/streets.json?key=public_key',
zoom: 2,
center: {"lat":10.762622,"lng":106.660172}
});
map.on('load', async () => {
// Get the initial location of the International Space Station (ISS).
const geojson = await getLocation();
// Add the ISS location as a source.
map.addSource('iss', {
type: 'geojson',
data: geojson
});
// Add the rocket symbol layer to the map.
map.addLayer({
'id': 'iss',
'type': 'symbol',
'source': 'iss',
'layout': {
'icon-image': 'rocket'
}
});
// Update the source from the API every 2 seconds.
const updateSource = setInterval(async () => {
const geojson = await getLocation(updateSource);
map.getSource('iss').setData(geojson);
}, 2000);
async function getLocation(updateSource) {
// Make a GET request to the API and return the location of the ISS.
try {
const response = await fetch(
'https://api.wheretheiss.at/v1/satellites/25544',
{ method: 'GET' }
);
const { latitude, longitude } = await response.json();
// Fly the map to the location.
map.flyTo({
center: [longitude, latitude],
speed: 0.5
});
// Return the location of the ISS as GeoJSON.
return {
'type': 'FeatureCollection',
'features': [
{
'type': 'Feature',
'geometry': {
'type': 'Point',
'coordinates': [longitude, latitude]
}
}
]
};
} catch (err) {
// If the updateSource interval is defined, clear the interval to stop updating the source.
if (updateSource) clearInterval(updateSource);
throw new Error(err);
}
}
});
</script>
</body>
</html>