Openlayers 3:以编程方式选择功能

Sco*_*ers 11 openlayers-3

我正在尝试将我的系统从Openlayers 2升级到Openlayers 3,我有一个我似乎无法弄清楚的特定问题.

我的应用程序有一个网格和一个地图,当用户点击网格时,我想选择地图上的相关点.

在Openlayers 2中,我使用了以下内容:

self.selectControl.select(feature[0]);
Run Code Online (Sandbox Code Playgroud)

我无法在Openlayers 3中找到或理解如何做同样的事情.

所以要清楚,我有一个我以编程方式找到的功能,我想在地图上选择该功能(以编程方式)!

我似乎无法在API中找到任何内容,但这可能是由于我不熟悉,因为我是Openlayers的新手.

Sco*_*ers 17

为此,您需要执行以下操作:

mySelectControl.getFeatures().clear() -> removes the selected items

mySelectControl.getFeatures().push(featureToSelect) -> selects the applied feature
Run Code Online (Sandbox Code Playgroud)


Fua*_*All 6

var selectInteraction = new ol.interaction.Select(}); 
map.addInteraction(selectInteraction);

function highlightFeature(feat){
   selectInteraction.getFeatures().push(feat);
   selectInteraction.dispatchEvent({
      type: 'select',
      selected: [feat],
      deselected: []
   });
}
Run Code Online (Sandbox Code Playgroud)

在最新的openlayers 4.5上像char一样工作