MapSnippets logoMapSnippets logo

Animated Data with MapTiler SDK

MapTiler SDK

Animate map features over time by updating paint properties in a requestAnimationFrame loop with MapTiler SDK.

progress_activityLoading map...
maptilersdk.config.apiKey = 'YOUR_MAPTILER_KEY';
const map = new maptilersdk.Map({
  container: 'map',
  style: maptilersdk.MapStyle.STREETS,
  center: [-98, 39],
  zoom: 3
});
map.on('load', () => {
  map.addSource('quakes', {
    type: 'geojson',
    data: 'https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_month.geojson'
  });
  map.addLayer({
    id: 'quake-circles', type: 'circle', source: 'quakes',
    paint: { 'circle-radius': 6, 'circle-color': '#9C27B0', 'circle-opacity': 0.7 }
  });
  const now = Date.now();
  function animate() {
    const elapsed = ((Date.now() - now) % 10000) / 10000;
    const cutoff = Date.now() - (1 - elapsed) * 30 * 24 * 3600 * 1000;
    map.setFilter('quake-circles', ['>=', ['get', 'time'], cutoff]);
    requestAnimationFrame(animate);
  }
  animate();
});

How it works

This example animates earthquake data by time. A requestAnimationFrame loop updates the layer’s filter to show only earthquakes after a moving time cutoff.

The cutoff slides through the past 30 days over a 10-second cycle. The modulo operation (% 10000) creates a repeating loop. Only earthquakes with a time property greater than the cutoff are visible.

This technique works for any dataset with timestamps. Replace the filter expression to match your data’s time property format.

Key APIs

API Description Docs
map.setFilter() Update layer filter dynamically docs.maptiler.com/sdk-js/api/
requestAnimationFrame Smooth animation loop MDN
['get', 'time'] Read feature timestamp property docs.maptiler.com/sdk-js/api/

FAQ

Can I use setPaintProperty instead of setFilter?

Yes. You can animate circle-opacity or circle-radius based on time for a fade-in effect instead of a hard cutoff.

How do I add a time slider?

Replace the requestAnimationFrame loop with an <input type="range"> that sets the cutoff value on change.

Common mistakes

  • Not using requestAnimationFrame: setInterval creates janky animation and wastes CPU when the tab is hidden
  • Forgetting the modulo for looping: without it, the animation runs once and stops
  • Updating the data source instead of the filter: setFilter is much more performant than setData for time-based visibility
auto_awesome

Build maps using MapTiler SDK with AI

Generate code like this animated data with maptiler sdk example using the MapTiler SDK skill in your IDE.