Fit a map to a bounding box

Use fitBounds to show a specific area of the map in view, regardless of the pixel size of the map.

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.STREETS, 
    center: [-74.5, 40],
    zoom: 9
});

document.getElementById('fit').addEventListener('click', function () {
    map.fitBounds([
        [32.958984, -5.353521],
        [43.50585, 5.615985]
    ]);
});
Fit to the bounds of a lineString

Fit to the bounds of a lineString

Examples

Get the bounds of a lineString.

Restrict map panning to an area

Restrict map panning to an area

Examples

Prevent map panning by setting maximum bounds.

Fly to a location

Fly to a location

Examples

Smoothly interpolate between locations using the flyTo animation.

How to center map based on visitor’s country bounds

Center map by visitor’s country IP

Tutorials

This tutorial shows how to center map based on the visitor’s country bounds using the MapTiler Geolocation API.

Was this helpful?