Connect SwissMapWeb and external API

by swisstopo_karto

HTML

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

<link
  rel="stylesheet"
  href="https://unpkg.com/maplibre-gl@6.16.0/dist/maplibre-gl.css"
>

<script type="module">

  // ==========================================
  // 1. IMPORT MAPLIBRE
  // ==========================================

  import * as maplibregl from
    "https://unpkg.com/maplibre-gl@6.16.0/dist/maplibre-gl.mjs";


  // ==========================================
  // 2. CREATE THE MAP
  // ==========================================

  const map = new maplibregl.Map({
    container: "map",
    style:
      "https://vectortiles.geo.admin.ch/styles/ch.swisstopo.basemap.vt/style.json",
    center: [7.44238, 46.947368],
    zoom: 17
  });

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


  // ==========================================
  // 3. ADD THE SELECTED-STOP HIGHLIGHT
  // ==========================================

  map.on("load", () => {

    map.addSource("selected-stop", {
      type: "geojson",
      data: {
        type: "FeatureCollection",
        features: []
      }
    });

    map.addLayer({
      id: "selected-stop-highlight",
      type: "circle",
      source: "selected-stop",
      paint: {
        "circle-radius": 17,
        "circle-color": "#1685E8",
        "circle-opacity": 0.35,
        "circle-stroke-color": "#1685E8",
        "circle-stroke-width": 2,
        "circle-stroke-opacity": 0.8
      }
    });

  });


  // ==========================================
  // 4. HANDLE MAP CLICKS
  // ==========================================

  map.on("click", async (event) => {

    const features = map.queryRenderedFeatures(event.point);

    // Identify railway stations, tram stops and bus stops
    const stationFeature = features.find(feature =>

      (
        feature.layer.id === "poi_rank1" &&
        feature.properties?.subclass === "railway_station"
      ) ||

      (
       ...

CSS

```css
/* ======================================
   MAP
====================================== */

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

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


/* ======================================
   MODERN DEPARTURE POPUP
====================================== */

.maplibregl-popup-content {
  padding: 0;
  border-radius: 16px;
  overflow: hidden;
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.18);
}

.maplibregl-popup-close-button {
  font-size: 22px;
  padding: 8px 12px;
  color: #64748b;
  border-radius: 0 16px 0 0;
}

.maplibregl-popup-close-button:hover {
  background: #edf2f7;
  color: #172033;
}

.departure-popup {
  font-family: Arial, sans-serif;
  width: 290px;
  color: #172033;
}


/* Popup header */

.popup-header {
  padding: 18px 18px 14px;
  background: #f8fafc;
}

.popup-header h3 {
  margin: 0 25px 5px 0;
  font-size: 17px;
  font-weight: 700;
}

.popup-subtitle {
  color: #64748b;
  font-size: 12px;
}


/* Departure list */

.departure-list {
  padding: 4px 16px;
}

.departure-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 13px 2px;
  border-bottom: 1px solid #edf0f4;
}

.departure-item:last-child {
  border-bottom: none;
}


/* Line number badge */

.line-badge {
  min-width: 40px;
  height: 40px;
  border-radius: 10px;
  background: #e8f1fc;
  color: #1769aa;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 17px;
  font-weight: 700;
}


/* Destination and platform */

.departure-destination {
  flex: 1;
  min-width: 0;
}

.departure-destination strong {
  display: block;
  font-size: 13px;
  line-height: 1.4;
}

.departure-platform {
  color: #64748b;
  font-size: 11px;
  margin-top: 4px;
}


/* Departure time and delay */

.departure-time {
  text-align: right;
  font-size:...