Skip to content

Animate a Line

Animate a line by progressively adding coordinates to a GeoJSON source.

const API_KEY = 'toursprung';

    // Build a sine-wave path across Austria
    const steps = 500;
    const coords = [];
    for (let i = 0; i <= steps; i++) {
        const t = i / steps;
        const lng = 9.5 + t * 8;
        const lat = 47.5 + Math.sin(t * Math.PI * 4) * 1.0;
        coords.push([lng, lat]);
    }

    const map = new maptoolkit.Map({
        container: 'map',
        style: `https://static.maptoolkit.net/styles/toursprung/terrain.json?api_key=${API_KEY}`,
        center: [13.5, 47.5],
        zoom: 6,
        attributionControl: { compact: false }
    });

    map.addControl(new maptoolkit.NavigationControl(), 'top-right');

    const geojson = { type: 'Feature', geometry: { type: 'LineString', coordinates: [] } };
    let step = 0;
    let animId;

    map.on('load', () => {
        map.addSource('line', { type: 'geojson', data: geojson });

        map.addLayer({
            id: 'line-animation',
            type: 'line',
            source: 'line',
            paint: { 'line-color': '#ee8a65', 'line-width': 3 }
        });

        function animate() {
            if (step < coords.length) {
                geojson.geometry.coordinates.push(coords[step++]);
                map.getSource('line').setData(geojson);
                animId = requestAnimationFrame(animate);
            }
        }
        animate();
    });

    document.getElementById('replay').addEventListener('click', () => {
        cancelAnimationFrame(animId);
        step = 0;
        geojson.geometry.coordinates = [];
        map.getSource('line').setData(geojson);
        function animate() {
            if (step < coords.length) {
                geojson.geometry.coordinates.push(coords[step++]);
                map.getSource('line').setData(geojson);
                animId = requestAnimationFrame(animate);
            }
        }
        animate();
    });
<!DOCTYPE html>
<html lang="en">
<head>
    <title>Animate a Line – Maptoolkit Maps JS</title>
    <meta property="og:description" content="Animate a line by progressively adding coordinates to a GeoJSON source." />
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <script src="https://unpkg.com/@maptoolkit/maps@11.0.0-beta.2/dist/maptoolkit.js"></script>
    <link rel="stylesheet" href="https://unpkg.com/@maptoolkit/maps@11.0.0-beta.2/dist/maptoolkit.css" />
    <style>
        html, body { width: 100%; height: 100%; margin: 0; padding: 0; }
        #map { width: 100%; height: 100%; }
        #replay {
            position: absolute;
            top: 10px;
            left: 10px;
            padding: 10px 20px;
            background: #ee8a65;
            color: white;
            border: none;
            border-radius: 3px;
            cursor: pointer;
            font-size: 14px;
        }
    </style>
</head>
<body>
<div id="map"></div>
<button id="replay">Replay</button>
<script>
    const API_KEY = 'toursprung';

    // Build a sine-wave path across Austria
    const steps = 500;
    const coords = [];
    for (let i = 0; i <= steps; i++) {
        const t = i / steps;
        const lng = 9.5 + t * 8;
        const lat = 47.5 + Math.sin(t * Math.PI * 4) * 1.0;
        coords.push([lng, lat]);
    }

    const map = new maptoolkit.Map({
        container: 'map',
        style: `https://static.maptoolkit.net/styles/toursprung/terrain.json?api_key=${API_KEY}`,
        center: [13.5, 47.5],
        zoom: 6,
        attributionControl: { compact: false }
    });

    map.addControl(new maptoolkit.NavigationControl(), 'top-right');

    const geojson = { type: 'Feature', geometry: { type: 'LineString', coordinates: [] } };
    let step = 0;
    let animId;

    map.on('load', () => {
        map.addSource('line', { type: 'geojson', data: geojson });

        map.addLayer({
            id: 'line-animation',
            type: 'line',
            source: 'line',
            paint: { 'line-color': '#ee8a65', 'line-width': 3 }
        });

        function animate() {
            if (step < coords.length) {
                geojson.geometry.coordinates.push(coords[step++]);
                map.getSource('line').setData(geojson);
                animId = requestAnimationFrame(animate);
            }
        }
        animate();
    });

    document.getElementById('replay').addEventListener('click', () => {
        cancelAnimationFrame(animId);
        step = 0;
        geojson.geometry.coordinates = [];
        map.getSource('line').setData(geojson);
        function animate() {
            if (step < coords.length) {
                geojson.geometry.coordinates.push(coords[step++]);
                map.getSource('line').setData(geojson);
                animId = requestAnimationFrame(animate);
            }
        }
        animate();
    });
</script>
</body>
</html>