Add custom zoom control

The custom zoom control in this example shows the exact current zoom level on a map. It helps set up zoom-based style rules or understand data resolution.

Adding custom controls can make your application stand out. Before you start making your controls, check out the premade MapTiler SDK controls (Geolocate, Scale, terrain, …).

NPM module setup


npm install --save @maptiler/sdk
import { Map, config } from '@maptiler/sdk';
import '@maptiler/sdk/dist/maptiler-sdk.css';

// Initialize MapTiler SDK
config.apiKey = "YOUR_MAPTILER_API_KEY_HERE";

// Initialize the map
const map = new Map({
  container: 'map', // The ID of the HTML element where the map will be rendered
});

// --- Custom Map Control Implementation ---

// This class will define how your custom control behaves and looks
// Read more about Icontrol https://docs.maptiler.com/sdk-js/api/controls/#icontrol
class ZoomControl {
  onAdd(map) {
    this._map = map;
    this._container = document.createElement('div'); // Create a new div element
    this._container.className = 'maplibregl-ctrl custom-map-control'; // Add SDK's control class and your custom class

    this._updateZoomHandler = () => {
      this._container.innerHTML = `${map.getZoom().toFixed(2)}`;
    };
    // Initial text with current zoom
    this._updateZoomHandler();
    // Add event listener for zoom changes
    map.on('zoomend', this._updateZoomHandler);
    return this._container;
  }

  onRemove() {
    this._container.parentNode.removeChild(this._container);
    this._map = undefined;
  }
}

map.addControl(new ZoomControl(), 'top-right');
Geocoding: Add place search to map

Geocoding control

Tutorials

Search for places using the map geocoding control component.

How to display a minimap or overview map control to aid the map navigation

Minimap control

Tutorials

Display an overview minimap control to aid map navigation.

Add a custom control programmatically

Custom control programmatically

Examples

Programmatically add custom controls for dynamic map logic integration.

Scale control display

Scale control

Tutorials

Learn to add and display map scale control.

Was this helpful?