Leaflet built-in basemap styles

This tutorial shows how to change the basemap (background map) style. Our built-in styles are designed to make it easier for you to create beautiful maps, without the need for extra coding or worrying about outdated versions. Check out the list of built-in styles.

NPM module setup


npm install --save @maptiler/leaflet-maptilersdk leaflet
import { MaptilerLayer, MapStyle } from '@maptiler/leaflet-maptilersdk';
import L from 'leaflet';
import 'leaflet/dist/leaflet.css';

const key = 'YOUR_MAPTILER_API_KEY_HERE';

const map = L.map('map').setView([50, 20], 4);
const mtLayer = new MaptilerLayer({
  apiKey: key,
  style: MapStyle.STREETS, // optional
}).addTo(map);

document.getElementById('buttons').addEventListener('click', function (event) {
  let style = MapStyle.STREETS;
  // Retrieve basemap style based on button id
  switch (event.target.id) {
    case "streets":
      style = MapStyle.STREETS;
      break;
    case "outdoor":
      style = MapStyle.OUTDOOR;
      break;
    case "dark":
      style = MapStyle.DATAVIZ.DARK;
      break;
    case "light":
      style = MapStyle.DATAVIZ.LIGHT;
      break;
  }
  mtLayer.setStyle(style);
});

Replace YOUR_MAPTILER_API_KEY_HERE with your own API key. Make sure to secure the key before you publish it.

How to display a custom map on a web with Leaflet

Custom Map

Tutorials

Display custom MapTiler maps on web pages with Leaflet JS.

Leaflet multi-lingual map

Multi-lingual map

Examples

Change the default map label language in Leaflet JS.

Was this helpful?