繁体   English   中英

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

[英]Openlayers 3: Select a feature programmatically

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

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

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

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

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

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

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

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

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

mySelectControl.getFeatures().push(featureToSelect) -> selects the applied feature
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一样

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

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

     selectInteractions.getFeatures().push(featureToSelect); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM