Add live realtime data

Use live real-time GeoJSON data streams to move a symbol on your map.

NPM module setup


npm install --save @maptiler/sdk
import { Map, MapStyle, config } from '@maptiler/sdk';
import '@maptiler/sdk/dist/maptiler-sdk.css';

config.apiKey = 'YOUR_MAPTILER_API_KEY_HERE';
const map = new Map({
    container: 'map',
    style: MapStyle.STREETS, 
    zoom: 0
});

const url = 'https://api.wheretheiss.at/v1/satellites/25544';
map.on('load', function () {
    const request = new XMLHttpRequest();
    window.setInterval(function () {
        // 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);

                const { latitude, longitude } = json;
                const data = {
                    'type': 'FeatureCollection',
                    'features': [
                        {
                        'type': 'Feature',
                        'geometry': {
                            'type': 'Point',
                            'coordinates': [longitude, latitude]
                        }
                        }
                    ]
                }

                // update the drone symbol's location on the map
                map.getSource('drone').setData(data);

                // fly the map to the drone's current location
                map.flyTo({
                    center: [longitude, latitude],
                    zoom: 7, 
                    speed: 0.5
                });
            }
        };
        request.send();
    }, 2000);

    map.addSource('drone', { type: 'geojson', data: url });
    map.addLayer({
        'id': 'drone',
        'type': 'symbol',
        'source': 'drone',
        'layout': {
            'icon-image': 'aquarium'
        }
    });
});
Update a feature in realtime

Update a feature in realtime

Examples

Change an existing feature on your map in real-time by updating its data.

Animate a point

Animate a point

Examples

Animate a point position using GeoJSON frame updates.

Was this helpful?