All examples

Draw a geofence

Click points on the map to draw a polygon, and get it back when it closes.

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>Draw a geofence</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; }

      .panel { position: absolute; top: 12px; left: 12px; z-index: 1; display: flex; flex-wrap: wrap; align-items: center; gap: 6px; max-width: calc(100% - 24px); }
      button { border: 0; border-radius: 999px; padding: 8px 14px; background: #fff; color: #161616; font: 500 13px Geist, system-ui, sans-serif; cursor: pointer; box-shadow: 0 1px 2px rgb(0 0 0 / 0.05), 0 0 0 1px rgb(0 0 0 / 0.1); transition: background-color 150ms cubic-bezier(0.215, 0.61, 0.355, 1); }
      button:hover { background: #f4f4f4; }
      button[aria-pressed="true"] { background: #191715; color: #faf9f7; }

      .status { padding: 8px 14px; border-radius: 999px; background: #fff; box-shadow: 0 1px 2px rgb(0 0 0 / 0.04), 0 0 0 1px rgb(0 0 0 / 0.07); }
    </style>
  </head>
  <body>
    <div id="map"></div>
    <div class="panel">
      <button id="draw">Draw a fence</button>
      <button id="clear">Clear</button>
      <span class="status">Click "Draw a fence", then click the map</span>
    </div>
    <script>
      const gebeta = new GebetaMaps({ apiKey: "YOUR_API_KEY" });

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

      const status = document.querySelector(".status");
      const draw = document.querySelector("#draw");

      map.on("load", () => {
        gebeta.fencing.on("fenceCompleted", (event) => {
          draw.setAttribute("aria-pressed", "false");
          map.getCanvas().style.cursor = "";
          status.textContent = `Fence ${event.fenceId} has ${event.points.length - 1} corners`;
        });
      });

      draw.addEventListener("click", () => {
        gebeta.fencing.startDrawing({ fillColor: "#2563eb", lineColor: "#2563eb" });
        draw.setAttribute("aria-pressed", "true");
        map.getCanvas().style.cursor = "crosshair";
        status.textContent = "Click to add corners. Click the first one to close.";
      });

      document.querySelector("#clear").addEventListener("click", () => {
        gebeta.fencing.clearAllFences();
        draw.setAttribute("aria-pressed", "false");
        map.getCanvas().style.cursor = "";
        status.textContent = "Cleared";
      });
    </script>
  </body>
</html>