17

OpenLayers, by default, zooms in when a user double clicks a map.

What is the best way to disable this behavior?

3 Answers 3

15

The zoom on double click feature is apparently a functionality in the OpenLayers.Control.Navigation control. See the OpenLayers Reference for more information.

A small and very tired example:

var Navigation = new OpenLayers.Control.Navigation({
    defaultDblClick: function(event) { return; }
});

I remember that the Navigation control is automatically added to the map if no controls are set during the initialization of the map. So you might have to add the Navigation control your self.

Hope it helps =)

1
  • Note that you apply this to the map like so map.addControl(Navigation); Commented Nov 1, 2018 at 9:37
11

OpenLayers 3 Documentation Link.

Static way:

var map = new ol.Map({
    interactions: ol.interaction.defaults({ doubleClickZoom: false }),
    ...
});

Dynamic way:

var interactions = map.getInteractions();
for (var i = 0; i < interactions.getLength(); i++) {
    var interaction = interactions.item(i);                          
    if (interaction instanceof ol.interaction.DoubleClickZoom) {
        map.removeInteraction(interaction);
        break;
    }
}
1
  • This is better than the accepted answer. Thank you +1
    – arm
    Commented Jul 23, 2020 at 7:28
7

The above answer is correct, but you will have to explicitly add this control to the map to override the default Navigation control, ie,

var Navigation = new OpenLayers.Control.Navigation({
  defaultDblClick: function(event) { return; }
});

map.addControl(Navigation);

The following controls are added by default to an OpenLayers.Map: OpenLayers.Control.Navigation, OpenLayers.Control.PanZoom, OpenLayers.Control.ArgParser, OpenLayers.Control.Attribution

So another option if you want to turn all these default behaviors off, is to send an empty array in the options parameter of the open layers map constructor, see link text for more details.

1
  • What does not work: changing the defaultDblClick method on the map's navigation control instance.
    – Lokomotywa
    Commented May 7, 2020 at 11:57

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