3

I need to disable all map interactions, like map zoom, drag, scroll zoom, any kind of interaction.

Is there some easy way to do it?

5 Answers 5

4

I didn't try this code but I hope it does the work :

var Interactions=yourMap.getInteractions();
for(var interaction in Interactions){
    yourMap.removeInteraction(interaction);
}

this is for OL3

Edits :

when declaring the map, set the interactions to false within its attributes :

interactions: ol.interaction.defaults({
    doubleClickZoom :false,
    dragAndDrop: false,
    keyboardPan: false,
    keyboardZoom: false,
    mouseWheelZoom: false,
    pointer: false,
    select: false
}),

and then using the method

ol.interaction.setActive(true);

you can re-enable them again, somehow setActive(false) didn't work for me, so here is a work around if it suits your needs

2
  • Nope, still I can drag and zoom. Commented Jun 2, 2016 at 15:47
  • how about defining the interactions "false" when you declare the map like this : interactions: ol.interaction.defaults({doubleClickZoom :false}), and so one for each interaction : dragAndDrop keyboardPan keyboardZoom mouseWheelZoom pointer select Commented Jun 2, 2016 at 16:24
3

If you don't want any interactions, just put interactions:null into your map constructor like that :

var map = new ol.Map({
   interaction:null
});

If you don't want zoom buttons and info button, just put controls: null into your map constructor again

var map = new ol.Map({
   interaction: null,
   control: null
});

There you go : a working fiddle for example

1
  • is interactions not interaction
    – mariovials
    Commented Jul 2, 2019 at 21:16
3

map.getInteractions().forEach(x => x.setActive(false));

Works also with setActive(true) to re-enable interactions.

2

Openlayers 5

var map = new ol.Map({
  ...
  controls: [],
  interactions: []
});
-1

Shamelessly copied from another answer. Do this for each interaction type (MouseWheelZoom, PinchZoom, etc.)

var dblClickInteraction;
// find DoubleClickZoom interaction
map.getInteractions().getArray().forEach(function(interaction) {
  if (interaction instanceof ol.interaction.DoubleClickZoom) {
    dblClickInteraction = interaction;
  }
});
// remove from map
map.removeInteraction(dblClickInteraction);
2
  • 1
    please add link to the other answer
    – tinlyx
    Commented Jun 15, 2018 at 21:19
  • Welcome to GIS SE! As a new user be sure to take the Tour to learn about our focussed Q&A format.
    – PolyGeo
    Commented Jun 16, 2018 at 0:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.