Skip to content

Change the Map Style

Call map.setStyle() with a different style URL to switch cartography without recreating the map. This example wires up buttons for the six standard community styles.

import { MaptoolkitLogoControl } from 'https://cdn.jsdelivr.net/npm/@maptoolkit/maplibre-gl-logo/+esm';
function styleUrl(slug) {
  return 'https://styles.maptoolkit.org/' + slug + '.json';
}
const styles = [
  { label: 'Summer', slug: 'summer' },
  { label: 'Light', slug: 'light' },
  { label: 'Hiking', slug: 'hiking' },
  { label: 'Winter', slug: 'winter' },
  { label: 'Dark', slug: 'dark' },
  { label: 'Street', slug: 'street' }
];
const map = new maplibregl.Map({ container: 'map', attributionControl: { customAttribution: '' }, style: styleUrl('summer'), center: [8.5417, 47.3769], zoom: 11 }); // Zurich
const switcher = document.getElementById('switcher');
styles.forEach(function (s, i) {
  const btn = document.createElement('button');
  btn.textContent = s.label;
  if (i === 0) btn.classList.add('active');
  btn.addEventListener('click', function () {
    map.setStyle(styleUrl(s.slug));
    switcher.querySelectorAll('button').forEach(function (b) { b.classList.remove('active'); });
    btn.classList.add('active');
  });
  switcher.appendChild(btn);
});
map.addControl(new MaptoolkitLogoControl());
<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/maplibre-gl.js"></script>
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/maplibre-gl.css" />
  <style>
    html, body { width: 100%; height: 100%; margin: 0; padding: 0; }
    #map { width: 100%; height: 100%; }
    .style-switcher { position:absolute; top:10px; left:10px; z-index:1; background:#fff; border-radius:6px; padding:6px; box-shadow:0 1px 4px rgba(0,0,0,.3); font:13px/1.2 system-ui,sans-serif; }
    .style-switcher button { display:inline-block; margin:2px; padding:5px 9px; border:none; border-radius:4px; background:#f1f1f1; cursor:pointer; }
    .style-switcher button.active { background:#1a73e8; color:#fff; }
  </style>
</head>
<body>
  <div class="style-switcher" id="switcher"></div>
  <div id="map"></div>
  <script type="module">
import { MaptoolkitLogoControl } from 'https://cdn.jsdelivr.net/npm/@maptoolkit/maplibre-gl-logo/+esm';
function styleUrl(slug) {
  return 'https://styles.maptoolkit.org/' + slug + '.json';
}
const styles = [
  { label: 'Summer', slug: 'summer' },
  { label: 'Light', slug: 'light' },
  { label: 'Hiking', slug: 'hiking' },
  { label: 'Winter', slug: 'winter' },
  { label: 'Dark', slug: 'dark' },
  { label: 'Street', slug: 'street' }
];
const map = new maplibregl.Map({ container: 'map', attributionControl: { customAttribution: '' }, style: styleUrl('summer'), center: [8.5417, 47.3769], zoom: 11 }); // Zurich
const switcher = document.getElementById('switcher');
styles.forEach(function (s, i) {
  const btn = document.createElement('button');
  btn.textContent = s.label;
  if (i === 0) btn.classList.add('active');
  btn.addEventListener('click', function () {
    map.setStyle(styleUrl(s.slug));
    switcher.querySelectorAll('button').forEach(function (b) { b.classList.remove('active'); });
    btn.classList.add('active');
  });
  switcher.appendChild(btn);
});
map.addControl(new MaptoolkitLogoControl());
  </script>
</body>
</html>