Navigate the map with game-like controls

Use the keyboard’s arrow keys to move around the map with game-like controls.

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.TOPO, 
    center: [-87.6298, 41.8781],
    zoom: 17,
    bearing: -12,
    pitch: 60,
    interactive: false
});

// pixels the map pans when the up or down arrow is clicked
const deltaDistance = 100;

// degrees the map rotates when the left or right arrow is clicked
const deltaDegrees = 25;

function easing(t) {
    return t * (2 - t);
}

map.on('load', function () {
    map.getCanvas().focus();

    map.getCanvas().addEventListener(
        'keydown',
        function (e) {
            e.preventDefault();
            if (e.which === 38) {
                // up
                map.panBy([0, -deltaDistance], {
                    easing: easing
                });
            } else if (e.which === 40) {
                // down
                map.panBy([0, deltaDistance], {
                    easing: easing
                });
            } else if (e.which === 37) {
                // left
                map.easeTo({
                    bearing: map.getBearing() - deltaDegrees,
                    easing: easing
                });
            } else if (e.which === 39) {
                // right
                map.easeTo({
                    bearing: map.getBearing() + deltaDegrees,
                    easing: easing
                });
            }
        },
        true
    );
});
Animate map camera around a point

Animate map camera around a point

Examples

Animate the map camera rotating around a specific point.

Creates an animated map to fly between different locations

Creates an animated map to fly between different locations

Examples

Animate map flights between locations using a heatmap layer.

Create a story map, fly to a location based on the scroll position

Fly to a location based on the scroll position

Examples

Fly to locations on the map by scrolling.

Was this helpful?