Draw Shapes - Handling Events

Register event handler on drawn shapes.

  1. Example
  2. Running the Sample Locally

Example

Draw Shapes - Handling Events
        // Initialize and add the map
let map: woosmap.map.Map;
const events = [
  "draw.create",
  "draw.delete",
  "draw.modechange",
  "draw.selectionchange",
  "draw.update",
  "draw.combine",
  "draw.uncombine",
];

const data: woosmap.map.GeoJSONFeatureCollection = {
  type: "FeatureCollection",
  features: [
    {
      id: "c40c2c23ce0c89dbc445b4dcc5913754",
      type: "Feature",
      properties: {},
      geometry: {
        coordinates: [
          [
            [-98.73532642865247, 30.126769329207107],
            [-99.58823422826457, 29.363810063756134],
            [-97.94703285628337, 28.640467970538566],
            [-96.84859099314622, 28.41338984007197],
            [-96.04737457532838, 28.867055658752506],
            [-95.37538661199761, 29.330016761277975],
            [-94.54832450328229, 29.846944934629335],
            [-94.9230870212941, 30.750699730175043],
            [-97.93411001083463, 30.950398105638087],
            [-98.73532642865247, 30.126769329207107],
          ],
        ],
        type: "Polygon",
      },
    },
    {
      id: "56cc82cc895f6cb52d87f75a7e94a904",
      type: "Feature",
      properties: {},
      geometry: {
        coordinates: [
          [
            [-98.27139781722337, 33.638565671747955],
            [-98.25118921584728, 32.02551092926721],
            [-95.62407103697022, 32.05977071105772],
            [-95.62407103697022, 33.65538867007554],
            [-98.27139781722337, 33.638565671747955],
          ],
        ],
        type: "Polygon",
      },
    },
  ],
};

function populateTable() {
  const eventsTable = document.getElementById("sidebar");
  let content = "";
  for (let i = 0; i < events.length; i++) {
    content += `<div class="event" id="${events[i]}">${events[i]}</div>`;
  }
  if (eventsTable) {
    eventsTable.innerHTML = content;
  }
}

function setupListener(name) {
  const eventRow = document.getElementById(name);
  if (eventRow) {
    eventRow.className = "event active";
    setTimeout(() => {
      eventRow.className = "event inactive";
    }, 1000);
  }
}

function initMap() {
  map = new window.woosmap.map.Map(
    document.getElementById("map") as HTMLElement,
    {
      center: { lat: 31.54350013250679, lng: -97.13238669872247 },
      zoom: 6.5,
    },
  );

  const draw = new window.woosmap.map.Drawing({});
  draw.addControl(map);

  window.woosmap.map.event.addListenerOnce(map, "idle", () => {
    draw.add(data);
  });

  populateTable();

  for (let i = 0; i < events.length; i++) {
    const eventName = events[i] as MapboxDraw.DrawEventType;
    draw.addListener(eventName, () => {
      setupListener(eventName);
    });
  }
}

declare global {
  interface Window {
    initMap: () => void;
  }
}
window.initMap = initMap;

    
        // Initialize and add the map
let map;
const events = [
  "draw.create",
  "draw.delete",
  "draw.modechange",
  "draw.selectionchange",
  "draw.update",
  "draw.combine",
  "draw.uncombine",
];
const data = {
  type: "FeatureCollection",
  features: [
    {
      id: "c40c2c23ce0c89dbc445b4dcc5913754",
      type: "Feature",
      properties: {},
      geometry: {
        coordinates: [
          [
            [-98.73532642865247, 30.126769329207107],
            [-99.58823422826457, 29.363810063756134],
            [-97.94703285628337, 28.640467970538566],
            [-96.84859099314622, 28.41338984007197],
            [-96.04737457532838, 28.867055658752506],
            [-95.37538661199761, 29.330016761277975],
            [-94.54832450328229, 29.846944934629335],
            [-94.9230870212941, 30.750699730175043],
            [-97.93411001083463, 30.950398105638087],
            [-98.73532642865247, 30.126769329207107],
          ],
        ],
        type: "Polygon",
      },
    },
    {
      id: "56cc82cc895f6cb52d87f75a7e94a904",
      type: "Feature",
      properties: {},
      geometry: {
        coordinates: [
          [
            [-98.27139781722337, 33.638565671747955],
            [-98.25118921584728, 32.02551092926721],
            [-95.62407103697022, 32.05977071105772],
            [-95.62407103697022, 33.65538867007554],
            [-98.27139781722337, 33.638565671747955],
          ],
        ],
        type: "Polygon",
      },
    },
  ],
};

function populateTable() {
  const eventsTable = document.getElementById("sidebar");
  let content = "";

  for (let i = 0; i < events.length; i++) {
    content += `<div class="event" id="${events[i]}">${events[i]}</div>`;
  }

  if (eventsTable) {
    eventsTable.innerHTML = content;
  }
}

function setupListener(name) {
  const eventRow = document.getElementById(name);

  if (eventRow) {
    eventRow.className = "event active";
    setTimeout(() => {
      eventRow.className = "event inactive";
    }, 1000);
  }
}

function initMap() {
  map = new window.woosmap.map.Map(document.getElementById("map"), {
    center: { lat: 31.54350013250679, lng: -97.13238669872247 },
    zoom: 6.5,
  });

  const draw = new window.woosmap.map.Drawing({});

  draw.addControl(map);
  window.woosmap.map.event.addListenerOnce(map, "idle", () => {
    draw.add(data);
  });
  populateTable();

  for (let i = 0; i < events.length; i++) {
    const eventName = events[i];

    draw.addListener(eventName, () => {
      setupListener(eventName);
    });
  }
}

window.initMap = initMap;

    
        html,
body {
  height: 100%;
  margin: 0;
  padding: 0;
  font-family: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, Segoe UI, Roboto, Helvetica Neue, Arial, Noto Sans, sans-serif, Apple Color Emoji, Segoe UI Emoji, Segoe UI Symbol, Noto Color Emoji;
}

#container {
  height: 100%;
  display: flex;
}

#sidebar {
  flex-basis: 12rem;
  flex-grow: 1;
  max-width: 30rem;
  height: 100%;
  box-sizing: border-box;
  overflow: auto;
}

#map {
  flex-basis: 70vw;
  flex-grow: 5;
  height: 100%;
}

.events {
  font-family: "Droid Sans Mono", monospace;
}

.event {
  transition: background-color 0.5s ease-out;
  padding: 0.5em;
  overflow: hidden;
  font-size: 14px;
}

.active {
  background-color: #ffdd00;
  color: #000;
}

.inactive {
  background-color: white;
}


    
        <html>
  <head>
    <title>Draw Shapes - Handling Events</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta charset="utf-8" />

    <link rel="stylesheet" type="text/css" href="./style.css" />
    <script type="module" src="./index.js"></script>
  </head>
  <body>
    <div id="container">
      <div id="map"></div>
      <div class="events" id="sidebar"></div>
    </div>

    <script
      src="https://sdk.woosmap.com/map/map.js?key=woos-48c80350-88aa-333e-835a-07f4b658a9a4&callback=initMap&libraries=drawing"
      defer
    ></script>
  </body>
</html>

    

Running the Sample Locally

Before you can run this sample on your local machine, you need to have Git and Node.js installed. If they’re not already installed, follow these instructions to get them set up.

Once you have Git and Node.js installed, you can run the sample by following these steps:

  1. Clone the repository and navigate to the directory of the sample.

  2. Install the necessary dependencies.

  3. Start running the sample.

Here are the commands you can use in your terminal to do this:

Shell
        git clone -b sample/draw-shapes-events https://github.com/woosmap/js-samples.git
cd js-samples
npm i
npm start

    

You can experiment with other samples by switching to any branch that starts with the pattern sample/SAMPLE_NAME.

Shell
        git checkout sample/SAMPLE_NAME
npm i
npm start

    
Was this article helpful?
Have more questions? Submit a request