Interaction
Quick answer
Interaction is ol.interaction.Interaction, the base class for every mouse and touch behavior on a map. Its active option and setActive method enable or disable a handler, while handleEvent decides whether an event is consumed and stops propagation. Extend Pointer or Interaction to build custom gestures, then register with map.addInteraction.
new ol.interaction.Interaction( options )
Key points
- setActive(false) disables the handler without unloading it, so state resumes when re-enabled.
- map.addInteraction appends after default interactions; insertAt(0, interaction) preempts them.
- handleEvent returns true when the event is consumed, truncating later handlers.
Example
map.getInteractions().forEach(function (interaction) {
if (interaction instanceof ol.interaction.DoubleClickZoom) {
interaction.setActive(false);
}
});
var dragBox = new ol.interaction.DragBox({
condition: function (evt) { return evt.originalEvent.shiftKey; }
});
map.addInteraction(dragBox);
dragBox.on('boxend', function () {
console.log(dragBox.getGeometry().getExtent());
});
Notes
- Handler order matters, so a custom interaction returning true early blocks Draw or Modify.
- Constructing the base Interaction directly produces no usable gesture; subclass Pointer instead.
English edition written for Atlaslog. Code is taken from the original Chinese page and runs unchanged.
Atlaslog (2026). Interaction. https://www.jingweizhi.com/en/openlayers/api/interaction.html (accessed 2026-11-03).