MapSnippets logoMapSnippets logo

Configure Interactions in OpenLayers

OpenLayers

Configure zoom, pan, and rotation interactions on an ol.Map using OpenLayers interaction defaults.

progress_activityLoading 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 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 of ol.interaction.defaults.defaults(...): in OL 10.x the UMD build exposes defaults as a namespace; the function lives one level deeper. 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.

Related Examples