单击并在Openlayers 3中双击并选择功能

时间:2019-05-13 10:36:32

标签: select onclick openlayers-3 double-click

我想在openlayers 3中同时单击并双击选择要素。创建ol.interaction.Select时的 condition 选项仅具有一个功能,因此需要一种变通办法< / p>

我试图编写自己的自定义条件函数来调用适当的函数,我在想类似...

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中这是否可能吗?如何处理这种情况?

非常感谢

1 个答案:

答案 0 :(得分:0)

该条件是接受地图浏览器事件并返回布尔值的函数。根据OpenLayers示例https://openlayers.org/en/v4.6.5/examples/select-features.html中的Alt + Cick来选择单击还是双击,您将需要

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,
    });