Skip to content

Animate a Point

Update a GeoJSON source with setData() inside requestAnimationFrame to animate a point. Here it orbits a center coordinate over Berlin.

import { MaptoolkitLogoControl } from 'https://cdn.jsdelivr.net/npm/@maptoolkit/maplibre-gl-logo/+esm';
const map = new maplibregl.Map({ container: 'map', attributionControl: { customAttribution: '' }, style: 'https://styles.maptoolkit.org/summer.json', center: [13.405, 52.52], zoom: 9 }); // Berlin
map.on('load', function () {
  map.addSource('pt', { type: 'geojson', data: { type: 'Feature', properties: {}, geometry: { type: 'Point', coordinates: [13.405, 52.52] } } });
  map.addLayer({ id: 'pt', type: 'circle', source: 'pt', paint: { 'circle-radius': 9, 'circle-color': '#e8551a', 'circle-stroke-width': 2, 'circle-stroke-color': '#fff' } });
  let a = 0;
  function frame() {
    a += 0.02;
    const c = [13.405 + Math.cos(a) * 0.06, 52.52 + Math.sin(a) * 0.06];
    map.getSource('pt').setData({ type: 'Feature', properties: {}, geometry: { type: 'Point', coordinates: c } });
    requestAnimationFrame(frame);
  }
  frame();
});
map.addControl(new MaptoolkitLogoControl());
<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/maplibre-gl.js"></script>
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/maplibre-gl.css" />
  <style>
    html, body { width: 100%; height: 100%; margin: 0; padding: 0; }
    #map { width: 100%; height: 100%; }
  </style>
</head>
<body>
  <div id="map"></div>
  <script type="module">
import { MaptoolkitLogoControl } from 'https://cdn.jsdelivr.net/npm/@maptoolkit/maplibre-gl-logo/+esm';
const map = new maplibregl.Map({ container: 'map', attributionControl: { customAttribution: '' }, style: 'https://styles.maptoolkit.org/summer.json', center: [13.405, 52.52], zoom: 9 }); // Berlin
map.on('load', function () {
  map.addSource('pt', { type: 'geojson', data: { type: 'Feature', properties: {}, geometry: { type: 'Point', coordinates: [13.405, 52.52] } } });
  map.addLayer({ id: 'pt', type: 'circle', source: 'pt', paint: { 'circle-radius': 9, 'circle-color': '#e8551a', 'circle-stroke-width': 2, 'circle-stroke-color': '#fff' } });
  let a = 0;
  function frame() {
    a += 0.02;
    const c = [13.405 + Math.cos(a) * 0.06, 52.52 + Math.sin(a) * 0.06];
    map.getSource('pt').setData({ type: 'Feature', properties: {}, geometry: { type: 'Point', coordinates: c } });
    requestAnimationFrame(frame);
  }
  frame();
});
map.addControl(new MaptoolkitLogoControl());
  </script>
</body>
</html>