[GIS] Disable pan and zoom in OpenLayers

openlayerspanzoom

I'm using the latest version of OpenLayers in my map application, and I need disable all zoom functions and panning for users. Zoom, mouse zoom, double click zoom, and control buttons zoom need to be disabled.

How can I do that? I've made some searches but I've just found how to disable everything, and I need other interactions like single click on a feature.

Best Answer

In OpenLayers 5 using ES modules:

import Map from 'ol/Map';
import View from 'ol/View';
import TileLayer from 'ol/layer/Tile';
import XYZ from 'ol/source/XYZ';
import {defaults as defaultInteractions} from 'ol/interaction.js';`

new Map({
  target:'map',
  layers: [
    new TileLayer({
      source: new XYZ({
        url: 'https://{a-c}.tile.openstreetmap.org/{z}/{x}/{y}.png'
      })
    })
  ],
  view: new View({
    center: [0,0],
    zoom: 2
  }),
  interactions: defaultInteractions({
    doubleClickZoom: false,
    dragAndDrop: false,
    dragPan: false,
    keyboardPan: false,
    keyboardZoom: false,
    mouseWheelZoom: false,
    pointer: false,
    select: false
  }),
})
Related Question