Add a custom control declarative way

Declarative controls offer a simple way to add interactive UI elements to the map by using HTML attributes alone. Instead of instantiating controls through JavaScript, developers annotate DOM elements and allow the SDK to discover and wire them automatically.

To activate declarative control detection set the customControls option to true in the map initialization configuration. Alternatively, customControls may be set to a CSS selector string, restricting autodetection to:

  • Elements matching the selector directly
  • Or elements whose ancestor matches the selector

NPM module setup


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

config.apiKey = 'YOUR_MAPTILER_API_KEY_HERE';
const map = new Map({
  container: 'map', // container's id or the HTML element to render the map
  style: MapStyle.STREETS,
  customControls: true,
  navigationControl: false,
  geolocateControl: false,
  terrainExaggeration: 4,
});

//custom control (center map)
document.querySelector(".home-button")?.addEventListener("click", () => map.easeTo({ center: [15, 50], zoom: 7 }));

Learn more

Check out the MaptilerExternalControl API reference to learn all the options to add custom controls to your map interface.

Add a custom control programmatically

Custom control programmatically

Examples

Programmatically add custom controls for dynamic map logic integration.

Add custom zoom control

Add custom zoom control

Examples

Add custom zoom level control to your map interface.

Hide map navigation controls

Hide map navigation controls

Tutorials

Hide the map navigation and zoom controls.

Scale control display

Scale control

Tutorials

Learn to add and display map scale control.

Was this helpful?