【问题标题】:How to disable DragPan in OpenLayers 3?如何在 OpenLayers 3 中禁用 DragPan?
【发布时间】:2015-03-17 10:50:23
【问题描述】:

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

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

【问题讨论】:

    标签: openlayers-3


    【解决方案1】:

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

    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

    【讨论】:

      【解决方案2】:

      Open Layers 3 中现在有一个setActive 方法:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-05-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多