How to disable DragPan in OpenLayers 3?

独自空忆成欢 提交于 2019-12-08 15:35:16

问题


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.


回答1:


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.




回答2:


There is now a setActive method in Open Layers 3:

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


来源:https://stackoverflow.com/questions/29097178/how-to-disable-dragpan-in-openlayers-3

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!