All examples

Route between clicked points

Click a start and an end on the map to get a route.

Code

Replace YOUR_API_KEY with a key from your Gebeta dashboard, save the file as index.html and open it in a browser.

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>Route between clicked points</title>
    <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Geist:wght@400;500&display=swap" />
    <script src="https://tiles.gebeta.app/static/v3/gebeta-maps.umd.js"></script>
    <style>
      body { margin: 0; font: 13px/1.4 Geist, system-ui, sans-serif; color: #161616; }
      #map { position: absolute; inset: 0; }

      .card { position: absolute; top: 12px; left: 12px; z-index: 1; max-width: calc(100% - 52px); padding: 10px 14px; border-radius: 8px; background: #fff; box-shadow: 0 1px 2px rgb(0 0 0 / 0.04), 0 0 0 1px rgb(0 0 0 / 0.07); }
      .card strong { display: block; font-weight: 500; }
      .card small { color: #8a8a8a; font-size: 13px; }

      .pin { width: 28px; height: 45px; filter: drop-shadow(0 0 1px #fff) drop-shadow(0 0 1px #fff); }
      .pin span { position: absolute; top: -4px; left: 20px; min-width: 16px; height: 16px; padding: 0 4px; box-sizing: border-box; border: 1px solid #fff; border-radius: 999px; background: #191715; color: #faf9f7; font: 500 10px/14px Geist, system-ui, sans-serif; text-align: center; }

      .label { margin-bottom: 6px; padding: 6px 10px; border-radius: 999px; background: #191715; color: #faf9f7; font: 500 13px/1 Geist, system-ui, sans-serif; white-space: nowrap; pointer-events: none; }
      .label::after { content: ""; position: absolute; top: 100%; left: 50%; transform: translateX(-50%); border: solid transparent; border-width: 6px 5px 0; border-top-color: #191715; }
    </style>
  </head>
  <body>
    <div id="map"></div>
    <div class="card"><strong>Click the map to set A</strong></div>
    <script>
      const INK = "#191715";

      function pin(color, label) {
        const element = document.createElement("div");
        element.className = "pin";
        element.innerHTML = `<svg width="28" height="45" viewBox="0 0 42 68"><path fill="${color}" d="M21 0a21 21 0 0 1 2 41.9V64a2 2 0 0 1-4 0V41.9A21 21 0 0 1 21 0Z"/><circle cx="21" cy="21" r="7" fill="#fff"/></svg>`;
        if (label) element.insertAdjacentHTML("beforeend", `<span>${label}</span>`);
        return element;
      }

      const { LngLatBounds, Marker } = window.gebeta;

      const gebeta = new GebetaMaps({ apiKey: "YOUR_API_KEY" });

      const map = gebeta.init({
        container: "map",
        center: [38.7578, 9.0192],
        zoom: 13,
      });

      const ROUTE_STYLE = { "line-color": INK, "line-width": 5, "line-opacity": 1 };

      function addCasing() {
        if (map.getLayer("route-casing")) return;
        map.addLayer(
          {
            id: "route-casing",
            type: "line",
            source: "gebeta-route",
            layout: { "line-join": "round", "line-cap": "round" },
            paint: { "line-color": "#fff", "line-width": 8 },
          },
          "gebeta-route"
        );
      }

      function halfway(coordinates) {
        const steps = coordinates.slice(1).map((point, i) => Math.hypot(point[0] - coordinates[i][0], point[1] - coordinates[i][1]));
        let remaining = steps.reduce((sum, step) => sum + step, 0) / 2;
        for (let i = 0; i < steps.length; i++) {
          if (remaining <= steps[i]) return coordinates[i + 1];
          remaining -= steps[i];
        }
        return coordinates[coordinates.length - 1];
      }

      function durationLabel(route) {
        const element = document.createElement("div");
        element.className = "label";
        element.textContent = route.duration;
        return new Marker({ element, anchor: "bottom" }).setLngLat(halfway(route.geometry.coordinates));
      }

      function fitRoute(route, left = 64) {
        const bounds = new LngLatBounds();
        route.geometry.coordinates.forEach((coordinate) => bounds.extend(coordinate));
        map.fitBounds(bounds, { padding: { top: 96, bottom: 48, left, right: 64 }, maxZoom: 16, duration: 800 });
      }

      const card = document.querySelector(".card");
      const startPin = new Marker({ element: pin(INK, "A"), anchor: "bottom" });
      const endPin = new Marker({ element: pin(INK, "B"), anchor: "bottom" });
      let label = null;
      let start = null;

      map.getCanvas().style.cursor = "crosshair";

      map.on("click", async (event) => {
        if (!start) {
          start = event.lngLat;
          gebeta.clearRoute();
          endPin.remove();
          label?.remove();
          startPin.setLngLat(start).addTo(map);
          card.innerHTML = "<strong>Now click to set B</strong>";
          return;
        }

        const end = event.lngLat;
        endPin.setLngLat(end).addTo(map);
        card.innerHTML = "<strong>Finding a route…</strong>";
        try {
          const route = await gebeta.getDirections(start, end);
          gebeta.displayRoute(route, { showMarkers: false, routeStyle: ROUTE_STYLE });
          addCasing();
          fitRoute(route);
          label = durationLabel(route).addTo(map);
          card.innerHTML = `<strong>${route.duration}</strong><small>${route.distance} · click to start again</small>`;
        } catch (error) {
          card.innerHTML = `<strong>No route</strong><small>${error.message}</small>`;
        }
        start = null;
      });
    </script>
  </body>
</html>