【问题标题】:Select features with click AND double-click in Openlayers 3在 Openlayers 3 中单击并双击选择要素
【发布时间】:2019-05-13 19:21:36
【问题描述】:

我想在openlayers 3中同时单击和双击选择特征。创建ol.interaction.Select时的条件选项只需要一个功能,因此需要解决方法

我尝试编写自己的自定义条件函数来调用适当的函数,我正在考虑类似...

this.selectType = (feature) => {
      if (feature){
        if(feature.onclick){
          return ol.events.condition.singleClick
        } else {
          return ol.events.condition.doubleClick
        }
      }
    }

this.selectInteraction = new ol.interaction.Select({
      condition: this.selectType(),
      toggleCondition: ol.events.condition.shiftKeyOnly,
      layers: this.layerFilter,
      features: this.features,
      style: this.selectStyle,
    });

...但没有成功。

我意识到,我可以创建两个单独的交互来选择功能,但我宁愿不这样做,因为这将涉及根据 Select 交互复制大量代码。

有谁知道这在 openlayers 中是否可行,以及如何处理这种情况?

非常感谢

【问题讨论】:

    标签: select onclick openlayers-3 double-click


    【解决方案1】:

    条件是一个接受地图浏览器事件并返回布尔值的函数。基于 OpenLayers 示例中的 Alt+Cick https://openlayers.org/en/v4.6.5/examples/select-features.html 在单击或双击时进行选择,您将需要类似

    this.selectType = (mapBrowserEvent) => {
              return ol.events.condition.singleClick(mapBrowserEvent) ||
                  ol.events.condition.doubleClick(mapBrowserEvent);
        }
    
    this.selectInteraction = new ol.interaction.Select({
          condition: this.selectType, // pass the function, don't call it!
          toggleCondition: ol.events.condition.shiftKeyOnly,
          layers: this.layerFilter,
          features: this.features,
          style: this.selectStyle,
        });
    

    【讨论】:

    • 谢谢,@Mike,它可以工作,虽然因为它定义了一个 mapBrowserEvent,所以现在禁用了双击缩放。只有当光标处有特征时才会尝试找到调用双击选择的方法
    • 当我编辑示例codepen.io/anon/pen/QRKMRe时,单人双工作都可以工作@
    • 是的,我确认您的解决方案有效。但它会禁用地图上的双击缩放,我也想包括在内
    • condition: ol.events.condition.click 会这样做
    • 谢谢@Mike,现在我有我需要的东西了
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-17
    • 2016-11-15
    • 2013-05-26
    • 1970-01-01
    相关资源
    最近更新 更多