All examples

Get coordinates on click

Show the latitude and longitude of wherever the map is clicked.

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>Get coordinates on click</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; }
      .card code { font: 13px ui-monospace, SFMono-Regular, monospace; }
    </style>
  </head>
  <body>
    <div id="map"></div>
    <div class="card"><strong>Click the map</strong><small>Its coordinates appear here.</small></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 { Marker } = window.gebeta;

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

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

      const card = document.querySelector(".card");
      const marker = new Marker({ element: pin(INK), anchor: "bottom" });

      map.on("click", (event) => {
        const { lng, lat } = event.lngLat;
        card.innerHTML = `<strong>Clicked point</strong><code>${lat.toFixed(6)}, ${lng.toFixed(6)}</code>`;
        marker.setLngLat(event.lngLat).addTo(map);
      });
    </script>
  </body>
</html>