Animate a marker

Update the location of a Marker on each frame to animate its position.

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,
    center: [0, 0],
    zoom: 2
});

const marker = new maptilersdk.Marker();

function animateMarker(timestamp) {
    const radius = 20;

    // Update the data to a new position based on the animation timestamp. The
    // divisor in the expression `timestamp / 1000` controls the animation speed.
    marker.setLngLat([
        Math.cos(timestamp / 1000) * radius,
        Math.sin(timestamp / 1000) * radius
    ]);

    // Ensure it's added to the map. This is safe to call if it's already added.
    marker.addTo(map);

    // Request the next frame of the animation.
    requestAnimationFrame(animateMarker);
}

// Start the animation.
requestAnimationFrame(animateMarker);
Animate a line

Animate a line

Examples

Animate lines by updating GeoJSON sources on every frame.

Add an animated icon to the map

Add an animated icon to the map

Examples

Add animated icons generated at runtime using Canvas API.

Animate a point along a route

Animate a point along a route

Examples

Smoothly animate a point along a route using Turf.

Was this helpful?