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

How to get all previously selected features in an OpenLayers map layer?

我有一个 OpenLayers 4 地图,带有单击交互和相应的 "select" 事件处理程序,该事件会更改所选要素样式。

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

每次选择新功能时,我都想设置所有以前选择的功能的样式。我想不通的是,当点击交互 "select" 事件被调用时,如何获取地图图层上所有先前选择的要素的集合?

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

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

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) {