Search code examples
openlayers-3

How to disable DragPan in OpenLayers 3?


How to disable DragPan interaction in Openlayers 3 (when map is already defined)?

Also, why I'm unable to use mousemove event?
I'm doing this: map.on('mousemove',function(e){ ...}); and it doesn't work.


Solution

  • To disable an interaction, you need to remove it from the map. If you don't have a reference to your interaction, you can find it using the getInteractions map method:

    var dragPan;
    map.getInteractions().forEach(function(interaction) {
      if (interaction instanceof ol.interaction.DragPan) {
        dragPan = interaction;
      }
    }, this);
    if (dragPan) {
      map.removeInteraction(dragPan);
    }
    

    For the mouse move event, the correct event to use is 'pointermove', see an example of use here: http://openlayers.org/en/v3.3.0/examples/icon.html

    Know that you can configure the interactions you want created and added by default to your map. If, for example, you wanted to create a map without the dragPan interaction, you could do so like this:

    var map = new ol.Map({
      layers: layers,
      interactions: ol.interaction.defaults({
        dragPan: false
      }),
      view: new ol.View({
        center: [0, 0],
        zoom: 2
      })
    });
    

    See here for a list of all possible options of ol.interaction.defaults.