All docsNMaps GL JS DocsExamplesRestrict map panning to an area

Restrict map panning to an area

This example prevents a map from being panned to a different location.

It uses maxBounds to set maximum geographical bounds, and pan and zoom operations are constrained within those bounds.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Restrict map panning to an area</title>
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" />
<script src="https://cdn.ndrive.com/nmapsgl/v1.6.6/nmaps-gl.js"></script>
<link href="https://cdn.ndrive.com/nmapsgl/v1.6.6/nmaps-gl.css" rel="stylesheet" />
<style>
body { margin: 0; padding: 0; }
#map { position: absolute; top: 0; bottom: 0; width: 100%; }
</style>
</head>
<body>
<div id="map"></div>
<script>
// TO MAKE THE MAP APPEAR YOU MUST ADD YOUR ACCESS TOKEN
nmapsgl.accessToken = '<your access token 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 nmapsgl.Map({
container: 'map',
style: 'Streets',
center: [-73.9978, 40.7209],
zoom: 13,
maxBounds: bounds // Sets bounds as max
});
</script>
</body>
</html>