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

时间:2014-10-17 10:02:35

标签: openlayers-3

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

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

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

self.selectControl.select(feature[0]);

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

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

我似乎无法在API中找到任何内容,但这可能是由于我对Openlayers的新手缺乏了解。

3 个答案:

答案 0 :(得分:16)

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

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

mySelectControl.getFeatures().push(featureToSelect) -> selects the applied feature

答案 1 :(得分:5)

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

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

就像最新的openlayers 4.5上的char一样。

答案 2 :(得分:1)

  1. 向地图添加精选互动。

    var selectInteraction = new ol.interaction.Select();
    map.addInteraction(selectInteraction);
    
  2. 将您要选择的所有要素添加到选择互动的要素数组中。

    selectInteractions.getFeatures().push(featureToSelect);
    
相关问题