Embed a SwissMapWeb map on your webpage

by swisstopo_karto

HTML

<link
  rel="stylesheet"
  href="https://cdnjs.cloudflare.com/ajax/libs/maplibre-gl/6.16.0/maplibre-gl.css"
>

<div class="map-wrapper">

  <!-- Map style buttons -->
  <div class="style-panel">
    <p class="panel-title">MAP STYLE</p>

    <button class="style-button active"
      data-style="basemap"
      aria-pressed="true">
      Basemap
    </button>

    <button class="style-button"
      data-style="winter"
      aria-pressed="false">
      Basemap Winter
    </button>

    <button class="style-button"
      data-style="light"
      aria-pressed="false">
      Light Basemap
    </button>

    <button class="style-button"
      data-style="imagery"
      aria-pressed="false">
      Imagery Basemap
    </button>
  </div>

  <div id="map"></div>

  <div id="map-status">Loading map...</div>

</div>

<script type="module">
  import * as maplibregl from
    "https://unpkg.com/maplibre-gl@6.16.0/dist/maplibre-gl.mjs";

  // Swisstopo map styles
  const styles = {
    basemap:
      "https://vectortiles.geo.admin.ch/styles/ch.swisstopo.basemap.vt/style.json",

    winter:
      "https://vectortiles.geo.admin.ch/styles/ch.swisstopo.basemap-winter.vt/style.json",

    light:
      "https://vectortiles.geo.admin.ch/styles/ch.swisstopo.lightbasemap.vt/style.json",

    imagery:
      "https://vectortiles.geo.admin.ch/styles/ch.swisstopo.imagerybasemap.vt/style.json"
  };

  // Initialize map
  const map = new maplibregl.Map({
    container: "map",
    style: styles.basemap,
    center: [8.23, 46.8],
    zoom: 8
  });

  // Navigation controls
  map.addControl(
    new maplibregl.NavigationControl(),
    "top-right"
  );

  const status = document.getElementById("map-status");
  const buttons = document.querySelectorAll(".style-button");

  // Map loading
  map.on("load", () => {
    status.textContent = "Map loaded";
  });

  // Error handling
  map.on("error", (event) => {
   ...

CSS

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
  font-family: Arial, sans-serif;
}

.map-wrapper {
  position: relative;
  width: 100%;
  height: 600px;
}

#map {
  width: 100%;
  height: 100%;
}

/* Compact floating style switcher */
.style-panel {
  position: absolute;
  top: 12px;
  left: 12px;
  z-index: 2;
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 5px;
  width: fit-content;
  padding: 7px;
  background: rgba(255, 255, 255, 0.96);
  border-radius: 7px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.2);
}

.panel-title {
  margin: 0 0 2px;
  font-size: 11px;
  font-weight: bold;
  color: #333;
}

.style-button {
  width: 100%;
  padding: 7px 8px;
  text-align: left;
  white-space: nowrap;
  font-size: 12px;
  color: #333;
  background: white;
  border: 1px solid #ccc;
  border-radius: 4px;
  cursor: pointer;
  transition: background 0.2s, color 0.2s, border-color 0.2s;
}

.style-button:hover {
  background: #f0f0f0;
}

.style-button.active {
  color: white;
  background: #245b9e;
  border-color: #245b9e;
  font-weight: bold;
}

.style-button:focus-visible {
  outline: 3px solid #f0a000;
  outline-offset: 2px;
}

/* Map status indicator */
#map-status {
  position: absolute;
  bottom: 12px;
  left: 12px;
  z-index: 2;
  padding: 7px 10px;
  background: rgba(255, 255, 255, 0.9);
  border-radius: 4px;
  font-size: 12px;
}

/* Mobile layout */
@media (max-width: 480px) {
  .style-panel {
    top: 10px;
    left: 10px;
    padding: 7px;
    gap: 4px;
  }

  .style-button {
    padding: 6px 8px;
    font-size: 11px;
  }

  .panel-title {
    font-size: 10px;
  }
}