Event Handling
MapWeave fires events in response to different events occuring 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 respond to user interactions occuring on any layer. See Events and Layers for more details.
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
if (item !== null) { // and not on the background
console.log(id); // log the item id
}
}
mapweave.on('click', clickHandler);
To detach it, use mapweave.off()
.
The available events and their event details are as follows:
Event | Passed object |
---|---|
click | ClickEvent object |
drag-end | DragEndEvent object |
drag-move | DragMoveEvent object |
drag-start | DragStartEvent object |
hover | HoverEvent object |
load | None |
view-change | None |
Take a look at the Interaction examples for more details.
Events and Layers
You can respond to user interactions occuring on any layer. See for instance the examples below:
- Highlight Network on Hover - Interaction with the network layer
- Highlight Trajectory on Hover - Interaction with the observations layer
- Filter Network by Region - Interaction with the GeoJSON layer
Note that if multiple layers overlap, the top layer is always picked during user interactions.
See also the Layers docs.
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);