Configure Interactions in OpenLayers
OpenLayersConfigure zoom, pan, and rotation interactions on an ol.Map using OpenLayers interaction defaults.
var map = new ol.Map({
target: 'map',
layers: [
new ol.layer.Tile({
source: new ol.source.XYZ({
url: 'https://api.maptiler.com/maps/streets-v4/256/{z}/{x}/{y}.png?key=YOUR_MAPTILER_KEY',
attributions: '© <a href="https://www.maptiler.com/copyright/">MapTiler</a>'
})
})
],
view: new ol.View({
center: ol.proj.fromLonLat([2.3522, 48.8566]),
zoom: 13
}),
interactions: ol.interaction.defaults.defaults({
doubleClickZoom: true,
dragPan: true,
mouseWheelZoom: true,
pinchZoom: true,
pinchRotate: false,
altShiftDragRotate: false
})
});How it works
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 | openlayers.org |
ol.Map |
Map constructor accepting interactions config | openlayers.org |
FAQ
How do I disable all zoom interactions?
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 ofol.interaction.defaults.defaults(...): in OL 10.x the UMD build exposesdefaultsas a namespace; the function lives one level deeper. Same applies tool.control.defaults.defaults(). - Forgetting that
altShiftDragRotateandpinchRotateare separate: disable both to fully prevent rotation - Not including
ol.proj.fromLonLat()when setting the center coordinates
Build maps using OpenLayers with AI
Generate code like this configure interactions in openlayers example using the OpenLayers skill in your IDE.
Related Examples
- Initialize an OpenLayers Map
- See how to do this in Leaflet, MapLibre GL JS or MapTiler SDK