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
);
});
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="robots" content="noindex">
<title>Navigate the map with game-like controls</title>
<link rel="stylesheet" href="./style.css">
</head>
<body>
<div id="map"></div>
<script type="module" src="./main.js"></script>
</body>
</html>
body {margin: 0; padding: 0;}
#map {position: absolute; top: 0; bottom: 0; width: 100%;}
Related examples
Creates an animated map to fly between different locations
ExamplesAnimate map flights between locations using a heatmap layer.