MapSnippets logo MapSnippets logo

Configure Interactions in OpenLayers

OpenLayers

Enable or disable OpenLayers map interactions such as wheel zoom, drag pan, and pinch rotate by passing options to ol.interaction.defaults.defaults.

progress_activity Loading map...
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: '&copy; <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 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 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 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

Build maps using OpenLayers with AI

Generate code like this configure interactions in openlayers example using the OpenLayers skill in your IDE.

Related examples