An event is some thing a browser does or a user does. An event can be a mouse click on button or a right click, double click etc. Map Events in OpenLayers are specific to map. These events can help the developers to achieve a particular task. For example, opening a feature information popup of a layer on map click. To enable that kind of functionality, first you need to activate the map click event.
In OpenLayers, there are two category of events:
- Map Events: Map events are specific to map
- Geometry Events: geometry events are specific to when drawing or modifying geometry
Following are the type of Map events in OpenLayers
- singleclick: A true single click with no dragging and no double click. This event is delayed 250ms to ensure that it’s not a double click
- postrender: Triggered after map is rendered
- pointermove: Triggered when pointer is moved
- pointerdrag: Triggered when pointer is dragged
- movestart: Triggered when map starts moving
- moveend: Triggered after map is moved
- dblclick: A true double click with no dragging
- click: A single click with no dragging. Double click will fire two events
Create Map Event Listeners:
To create an event listener, we will use map object defined at line number 1. When we define map in OpenLayers, it automatically will be shipped with many functions.
map.on is such function that will create an event listener. It accepts event type and anonymous function as parameters. At line number 16, we are creating a event listener for click.
Geometry events are used during feature interaction and modification. Geometry events are specific to when drawing or modifying geometry. Following are the type of Geometry events in OpenLayers
- drawstart: triggered upon feature draw start
- drawend: triggered upon feature draw end
- drawabort: triggered upon feature draw abortion
- modifystart: triggered upon feature modify start
- modifyend: triggered upon feature modify end
Create Geometry Event Listeners:
Geometry event listeners are used during feature interaction. For example, when you draw a line or polygon on map, you can use those events to perform some tasks on drawstart or drawend.
At line number 1, we are defining a draw interaction, that will enable drawing a line on map. Now we have a draw interaction object. draw.on is such function that will create an event listener. It accepts event type and anonymous function as parameters. At line number 6 and 10, we are creating an event listener for draw start and draw end.