Skip to content
Animate Map Camera Around a Point

Animate Map Camera Around a Point

Continuously rotate the map camera around a fixed point.

const API_KEY = 'toursprung';

    const map = new maptoolkit.Map({
        container: 'map',
        style: `https://static.maptoolkit.net/styles/toursprung/terrain.json?api_key=${API_KEY}`,
        center: [11.39085, 47.27574],
        zoom: 12,
        pitch: 55,
        bearing: 0,
        attributionControl: { compact: false }
    });

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

    let rotating = true;
    let animationId;

    function rotateCamera(timestamp) {
        map.rotateTo((timestamp / 100) % 360, { duration: 0 });
        animationId = requestAnimationFrame(rotateCamera);
    }

    map.on('load', () => {
        animationId = requestAnimationFrame(rotateCamera);
    });

    document.getElementById('toggle').addEventListener('click', () => {
        if (rotating) {
            cancelAnimationFrame(animationId);
            document.getElementById('toggle').textContent = 'Resume';
        } else {
            animationId = requestAnimationFrame(rotateCamera);
            document.getElementById('toggle').textContent = 'Pause';
        }
        rotating = !rotating;
    });
<!DOCTYPE html>
<html lang="en">
<head>
    <title>Animate Map Camera Around a Point – Maptoolkit Maps JS</title>
    <meta property="og:description" content="Continuously rotate the map camera around a fixed point." />
    <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%; }
        #toggle {
            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="toggle">Pause</button>
<script>
    const API_KEY = 'toursprung';

    const map = new maptoolkit.Map({
        container: 'map',
        style: `https://static.maptoolkit.net/styles/toursprung/terrain.json?api_key=${API_KEY}`,
        center: [11.39085, 47.27574],
        zoom: 12,
        pitch: 55,
        bearing: 0,
        attributionControl: { compact: false }
    });

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

    let rotating = true;
    let animationId;

    function rotateCamera(timestamp) {
        map.rotateTo((timestamp / 100) % 360, { duration: 0 });
        animationId = requestAnimationFrame(rotateCamera);
    }

    map.on('load', () => {
        animationId = requestAnimationFrame(rotateCamera);
    });

    document.getElementById('toggle').addEventListener('click', () => {
        if (rotating) {
            cancelAnimationFrame(animationId);
            document.getElementById('toggle').textContent = 'Resume';
        } else {
            animationId = requestAnimationFrame(rotateCamera);
            document.getElementById('toggle').textContent = 'Pause';
        }
        rotating = !rotating;
    });
</script>
</body>
</html>