How to enable or disable map interactions in OpenLayers
OpenLayers provides fine-grained control over map interactions through ol.interaction.defaults.defaults(). Each interaction type can be individually enabled or disabled.
This example disables rotation (both pinch and Alt+Shift+drag) while keeping all other interactions enabled. This is useful for applications where north-up orientation should be fixed.
The raster tile URL uses MapTiler’s streets-v4 style in PNG format, which works with the standard ol.source.XYZ source without needing ol-mapbox-style.
Key APIs
API
Description
Docs
ol.interaction.defaults.defaults()
Create default interaction collection with options
Set doubleClickZoom: false, mouseWheelZoom: false, and pinchZoom: false in the defaults. You can also set dragPan: false to make the map fully static.
Can I add custom interactions later?
Yes. Use map.addInteraction() to add any interaction after the map is created. Remove with map.removeInteraction().
Common mistakes
Calling ol.interaction.defaults(...) instead of ol.interaction.defaults.defaults(...). In OL 10.x the UMD build exposes defaults as a namespace, and the function lives one level deeper. The same applies to ol.control.defaults.defaults().
Forgetting that altShiftDragRotate and pinchRotate are separate. Disable both to fully prevent rotation.
Not including ol.proj.fromLonLat() when setting the center coordinates
auto_awesome
Build maps using OpenLayers with AI
Generate code like this configure interactions in openlayers example using the OpenLayers skill in your IDE.