8

I've searched for it on the entire web and finally I found on artamstrong.com how to disable a single feature like "mouseWheelZoom".

Here is how doubleclick zooming in OpenLayers 3 can be disabled:

var map = new ol.Map({
    controls : ol.control.defaults()
                .extend([ new ol.control.FullScreen() ]),
    interactions : ol.interaction.defaults({doubleClickZoom :false}),
    target : 'map',
    layers : [ new ol.layer.Tile({
            title : 'OpenStreetMaps',
            preload : Infinity,
            source : new ol.source.OSM(),
            visible : true
    }) ],
    view : new ol.View({
            center : ol.proj.transform([ 9.41, 48.82 ], 'EPSG:4326','EPSG:3857'),
            zoom : 12
    })
});

It's really that simple, just tell ol3 to set "doubleClickZoom" on false:

interactions : ol.interaction.defaults({doubleClickZoom :false})
  • 5
    Hi Brain, Welcome to GIS@Se, this is incredibly useful information, but this site requires the posting of specific questions and answers. Feel free to edit your question to make it a Question, and then answer it with your solution. People who post questions are able to answer their own questions. – Mark Cupitt Dec 18 '14 at 12:06
8

If you want to disable doubleClickZoom after map initialization, here is the way:

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);

You can also remove or add interaction such as PinchZoom, KeyboardZoom, etc,. with the same way.

  • In Array, you can use find instead of forEach: const dblClickInteractio = map.getInteraction().getArray().find((interaction ) => { return interaction instanceof ol.interaction.DoubleClickZoom }) – YairTawil Aug 17 '17 at 5:29
  • @YairTawil Good to know. but, is there an advantage to use find instead forEach? – Chase Choi Aug 17 '17 at 9:43
  • 1
    Sure, find will break the loop... forEach will always run n times – YairTawil Aug 20 '17 at 12:24
  • If there's multiple interactions, filter can be used as well. – Chase Choi Aug 21 '17 at 1:59
  • 1
    Should be map.getInteractions().getArray() with an 's' on the end of getInteractions(), not getInteraction. Though actually a much cleaner answer is in the question.... – mwarren Dec 10 '17 at 10:49
2

For OL 3 & 4 you can use ol.interaction.defaults as explained in the below answers:

https://stackoverflow.com/a/29967145/1225421

https://gis.stackexchange.com/a/195879/86845

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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