All docsNMaps GL JS DocsExamplesAdd live realtime data

Add live realtime data

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

<!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://cdn.ndrive.com/nmapsgl/v.1.6.8/nmaps-gl.js"></script>
<link href="https://cdn.ndrive.com/nmapsgl/v.1.6.8/nmaps-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>
// TO MAKE THE MAP APPEAR YOU MUST ADD YOUR ACCESS TOKEN
nmapsgl.accessToken = '<your access token here>';
const map = new nmapsgl.Map({
container: 'map',
style: 'Streets',
zoom: 0
});
map.on('load', () => {
map.loadImage(
'https://nmaps-gl.ndrive.com/nmaps-gl-js/assets/iss.png',
(error, image) => {
if (error) throw error;
map.addImage('iss', image);
const url = 'https://api.wheretheiss.at/v1/satellites/25544';
const request = new XMLHttpRequest();
window.setInterval(() => {
// make a GET request to parse the GeoJSON at the url
request.open('GET', url, true);
request.onload = function () {
if (this.status >= 200 && this.status < 400) {
// retrieve the JSON from the response
const json = JSON.parse(this.response);
// Fly the map to the location.
map.flyTo({
center: [json.longitude, json.latitude],
speed: 0.5
});
// Return the location of the ISS as GeoJSON.
const geojson = {
'type': 'FeatureCollection',
'features': [
{
'type': 'Feature',
'geometry': {
'type': 'Point',
'coordinates': [
json.longitude,
json.latitude
]
}
}
]
};
map.getSource('iss').setData(geojson);
}
};
request.send();
}, 5000);
// Add the ISS location as a source.
map.addSource('iss', {
type: 'geojson',
data: {
'type': 'FeatureCollection',
'features': []
}
});
// Add the rocket symbol layer to the map.
map.addLayer({
'id': 'iss',
'type': 'symbol',
'source': 'iss',
'layout': {
'icon-image': 'iss'
}
});
}
);
});
</script>
</body>
</html>