2.7. Setup map. Events
December 9, 2021 ยท View on GitHub
Theory
Every time something happens in NgwMap, e.g. user clicks or map zoom changes, the corresponding object sends an event which you can subscribe to with a function. It allows to react to user interaction.
Subscribe to all events via a special property:
ngwMap.emitter.on("moveend", onMapEvent);
Each object has its own set of events โ see documentation for details.
There are several ways to subscribe to layer events:
global:
ngwMap.emitter.on("layer:toggle", (ev) => {
if (ev.id === id) {
console.log("layer:toggle", ev);
}
});
or separately for each layer:
ngwMap.emitter.on("layer-" + id + ":toggle", (ev) => {
console.log("layer-" + id + ":toggle", ev);
});
More examples
Practice
- subscribe to different events using the
onandoncemethods; - learn to unsubscribe from events with
off(this is very important).
To run this example, you need to execute these commands in the terminal:
npm i
npm start
Open http://localhost:8080.
LAUNCH in the codesandbox.io.