我正在使用 OpenLayers v3.6 (这很重要,因为我找到的大多数解决方案都可以用于OpenLayers 2)。
我有一个表,当我在该表中选择一行时,我想在OpenLayers地图上突出显示/选择相应的功能。所有要素都是同一向量图层中的简单多边形( ol.geom.Polygon )。
。我设置了这样的选择互动:
// there is a lot of other code here
...
addSelectListener: function() {
this.SelectInteraction = new ol.interaction.Select({
condition: ol.events.condition.singleClick,
layers: function (layer) {
// defines layer from which features are selectable
return layer.get('id') == 'polygons_layer';
},
style: this.Style.Selected
});
// Map = ol.Map
Map.addInteraction(this.SelectInteraction);
this.SelectInteraction.on('select', this.selectPolygon, this);
}
...
selectPolygon: function(event) {
var selectSrc = this.getSelectInfo(event);
// some code that relies on selectSrc data
}
...
getSelectInfo: function (event) {
var selectSrc = {
deselected: null,
selected: null,
type: null
};
if (event.selected.length == 0 && event.deselected.length == 1) {
// click outside of polygon with previously selected
selectSrc.type = 'deselect';
selectSrc.deselected = {
feature: event.deselected[0],
id: event.deselected[0].getId()
};
} else if (event.deselected.length == 0 && event.selected.length == 1) {
// click on polygon without previously selected
selectSrc.type = 'select';
selectSrc.selected = {
feature: event.selected[0],
id: event.selected[0].getId()
}
} else if (event.deselected.length == 0 && event.selected.length == 1) {
// click on polygon with previously selected
selectSrc.type = 'switch';
selectSrc.deselected = {
feature: event.deselected[0],
id: event.deselected[0].getId()
};
selectSrc.selected = {
feature: event.selected[0],
id: event.selected[0].getId()
}
} else {
selectSrc.type = 'out';
}
return selectSrc;
}
当我想通过在地图上单击多边形来选择多边形时,此功能很好。但我想要的是实现同样的目标,而不是点击地图,而是点击地图外的一些元素(我的例子中的表行,但它可能是真的)。
我想使用select交互,因为发出的事件以及它应用于所选功能的样式。但是,如果有任何机会我可以在没有相同事件的情况下操纵选择交互中的选定功能,那就没问题。
我知道这个问题&amp;回答 - Openlayers 3: Select a feature programmatically - 但问题是我不能在评论中要求澄清(例如, mySelectControl 究竟是什么),因为我没有任何声誉:)< / p>
答案 0 :(得分:9)
要做的是linked问题。因此,将ol.Feature
推送到所选集合中:
var select = new ol.interaction.Select({
//some options
});
map.addInteraction(select);
var selected_collection = select.getFeatures();
selected_collection.push(featurePoint);
如果您想触发select
事件:
select.dispatchEvent('select');
// OR
select.dispatchEvent({
type: 'select',
selected: [featurePoly],
deselected: []
});