如何在OpenLayers 3中禁用DragPan?

时间:2015-03-17 10:50:23

标签: openlayers-3

如何在Openlayers 3中禁用DragPan交互(当已定义map时)?

另外,为什么我无法使用mousemove事件?
我这样做:map.on('mousemove',function(e){ ...});并且它无法正常工作。

2 个答案:

答案 0 :(得分:25)

要停用互动,您需要将其从地图中移除。如果您没有对交互的引用,可以使用getInteractions地图方法找到它:

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

对于鼠标移动事件,要使用的正确事件是“ pointermove ”,请参阅此处使用的示例:http://openlayers.org/en/v3.3.0/examples/icon.html

知道您可以配置您想要创建并在默认情况下添加到地图的交互。例如,如果您想在没有dragPan交互的情况下创建地图,则可以这样做:

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

有关ol.interaction.defaults的所有可能选项的列表,请参阅here

答案 1 :(得分:4)

现在打开图层3中有一个setActive方法:

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