Event Handling

MapWeave fires events in response to different events occurring on the map:

  • user interactions - for example, when the user drags a node or zooms in on the map
  • internal state changes - for example, when the map loads for the first time

You can find the events in the map adapter props.

You can respond to user interactions occurring on any layer.

To respond to an event, attach an event handler to it. When an event fires, the event handler is passed a single object containing all the event details. You can use destructuring to find the properties you need.

To attach an event handler, use the mapweave.on() function:

function clickHandler({ id, item }) {
  // if user clicked on an item and not on the background
  if (item !== null) {
    console.log(id); // log the item id
  }
}

mapweave.on('click', clickHandler);

To detach it, use mapweave.off().

To attach an event handler, include the event handler prop inside the MapWeave tag. In this case we are using onClick.

const clickHandler = ({ id, item }) => {
  // if user clicked on an item and not on the background
  if (item !== null) {
    console.log(id); // log the item id
  }
};

return (
  <MapWeave onClick={clickHandler}>
    <NetworkLayer data={networkData} />
  </MapWeave>
);

Events and Layers

You can respond to user interactions occurring on any layer. If multiple layers overlap, the top layer is always picked during user interactions. Layers are added in the order that they are created, so the first layer will be on the bottom and subsequent layers added on top of that.

Preventing Default Actions

To prevent the default action of the drag-start event, which is to create a dragger, call the preventDefault() function in the event handler:

function preventDrag({ preventDefault }) {
  preventDefault();
}

mapweave.on('drag-start', preventDrag);

To prevent the default action of the onDragStart event, which is to create a dragger, call the preventDefault() function in the event handler:

const preventDrag = ({ preventDefault }) => {
  preventDefault();
};

return (
  <MapWeave onDragStart={preventDrag}>
    <NetworkLayer data={networkData} />
  </MapWeave>
);

Terms of use

These terms do not alter or supersede any existing agreements between you (or your employer) and us.

By accessing or using any Content you agree to be bound by these Terms of Use. Please review these terms carefully before using the website.

The contents of this website, including but not limited to any text, code samples, API references, schemas, interactive tools, and other materials (collectively, the 'Content'), are made available for informational and internal evaluation purposes only. All intellectual property rights in the Content are reserved. No licence is granted to use the Content for any commercial purpose, or to copy, distribute, modify, reverse-engineer, or incorporate any part of the Content into any product or service, without our prior written consent.

This Content is provided “as is” and “as available,” without any representations, warranties, or guarantees of any kind, whether express or implied, including but not limited to implied warranties of merchantability, fitness for a particular purpose, non-infringement, or accuracy. To the fullest extent permitted by applicable law, we expressly exclude and disclaim all implied warranties, conditions, and other terms that might otherwise be implied.

We disclaim all liability for any loss or damage, whether direct, indirect, incidental, consequential, or otherwise, arising from any reliance placed on the Content or from your use of it, to the fullest extent permitted by applicable law. By continuing to access or use the Content, you acknowledge and agree to these terms.