如何在OpenLayers地图层中获取所有以前选择的功能?

如何在OpenLayers地图层中获取所有以前选择的功能?,openlayers,openlayers-3,Openlayers,Openlayers 3,我有一个OpenLayers 4映射,带有一个单击交互和一个相应的事件处理程序,用于“选择”事件,该事件更改所选功能样式 let clickInteraction = new olInteractionSelect({ layers: function (layer: any) { return layer.get('selectable') == true; }, condition: olEventCondition.singleClick, filter: (fea

我有一个OpenLayers 4映射,带有一个单击交互和一个相应的事件处理程序,用于“选择”事件,该事件更改所选功能样式

let clickInteraction = new olInteractionSelect({
  layers: function (layer: any) {
    return layer.get('selectable') == true;
  },
  condition: olEventCondition.singleClick,
  filter: (feature: any, layer: any) => {

    let shouldReturn: boolean = false;

    switch (layer.get(this.LAYER_PROPERTY_ID)) {
      case this.LAYER_ID_POINTS:
        this.onPointClick(feature.getId());
        shouldReturn = true;
        break;
      case this.LAYER_ID_AREAS:
        this.onAreaClick(feature.getId());
        shouldReturn = true;
        break;
      default:
        break;
    }
    return shouldReturn;
  }
});

let __this = this;

clickInteraction.on('select', function (evt: any) {
  let selected = evt.selected;
  let deselected = evt.deselected;

  //Select and set style for newly selected features
  if (selected.length) {
    selected.forEach(function (feature: any) {
      if (feature.get(__this.FEATUREKEY_SELECTEDSTYLEID)) {
        feature.setStyle(__this.createStyleFnFeatureSelected(feature.get(__this.FEATUREKEY_SELECTEDSTYLEID)));
      }
    });
  } else {
    deselected.forEach(function (feature: any) {
      if (feature.get(__this.FEATUREKEY_STYLEID)) {
        feature.setStyle(__this.createStyleFn(feature.get(__this.FEATUREKEY_STYLEID)));
      }
    });
  }
});

this.map.addInteraction(clickInteraction);
每次选择新特征时,我都要设置所有以前选择的特征的样式。我不明白的是,当点击交互“选择”事件被调用时,我如何获得地图层上所有先前选择的特征的集合


地图上有几千个特性,因此从性能角度来看,迭代所有特性是不可行的。我想要实现的是只获取以前选择的功能,而不是当前单击交互事件中选择或取消选择的功能

您可以简单地维护所选功能的队列,当选择新功能时,它们将添加到队列中。队列处理可以是可选的,但必须在添加新功能之前完成

let prevSelection = [];

clickInteraction.on('select', function (evt: any) {
  let selected = evt.selected;
  let deselected = evt.deselected;

  //Restyle previously selected features now if desired (or leave until next time)
  if (myCondition) {
    prevSelection.forEach(
      ...
    )
    prevSelection = [];
  }

  //Append newly selected features to queue for next time 
  prevSelection = prevSelection.concat(selected);

  //Select and set style for newly selected features
  if (selected.length) {

如果需要3种样式(从未选择、当前选择和以前选择),则需要删除
If(selected.length)
else
并无条件地处理两个forEach循环,因为单个事件可以包含新选择的功能和该操作取消选择的任何功能。如果只需要为“选定”和“未选定”指定单独的样式,则只需在交互构造函数中指定选定的样式,而不需要“选择”上的样式。为了澄清这一点,用户不会取消选择功能,因此它们不会在取消选择的集合中。我要做的是取消选择任何以前选择的功能,并以编程方式更改其样式。然后将样式应用于新选定的要素。因此,我试图在select活动开始时找出如何访问当前选定功能的集合。是的,昨晚我也在想同样的事情。谢谢