How to disable DragPan in OpenLayers 3?

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

Also, why can't I use the mousemove event?
I do this: map.on('mousemove',function(e){ ...}); and it does not work.

+9
openlayers-3
source share
3 answers

To disable interaction, you need to remove it from the card . If you don’t have a link to your interaction, you can find it using the getInteractions map getInteractions :

 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 the usage example here: http://openlayers.org/en/v3.3.0/examples/icon.html

Know that you can customize the interactions you want to create and add by default to your map. If, for example, you wanted to create a map without interacting with dragPan, you can do it as follows:

 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 the possible ol.interaction.defaults options.

+24
source share

In Open Layers 3, now setActive :

 map.getInteractions().forEach(function(interaction) { if (interaction instanceof ol.interaction.DragPan) { interaction.setActive(false); } }, this); 
+4
source share

For the solution only for mobile devices:

 map = new OpenLayers.Map({ div: "map", theme: null, projection: new OpenLayers.Projection("EPSG:900913"), numZoomLevels: 18, controls: [ new OpenLayers.Control.TouchNavigation({ dragPanOptions: { enableKinetic: true } }), new OpenLayers.Control.Zoom() ], layers: [ new OpenLayers.Layer.OSM("OpenStreetMap", null, { transitionEffect: 'resize' }) ] }); map.setCenter(new OpenLayers.LonLat(0, 0), 3); 
0
source share

All Articles