Restrict map panning to an area
Prevent a map from being panned to a different place by setting maxBounds.
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';
// Set bounds to New York, New York
const bounds = [
[-74.04728500751165, 40.68392799015035], // Southwest coordinates
[-73.91058699000139, 40.87764500765852] // Northeast coordinates
];
const map = new Map({
container: 'map',
style: MapStyle.STREETS,
center: [-73.9978, 40.7209],
zoom: 13,
maxBounds: bounds // Sets bounds as max
});
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="robots" content="noindex">
<title>Restrict map panning to an area</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
Center map by visitor’s country IP
TutorialsThis tutorial shows how to center map based on the visitor’s country bounds using the MapTiler Geolocation API.