简体   繁体   English

Openlayers 3:如何使用ol.interaction.Select以编程方式选择功能?

[英]Openlayers 3: how to select a feature programmatically using ol.interaction.Select?

I'm using OpenLayers v3.6 (this is important, because most of solutions that I found and would potentialy work are for OpenLayers 2). 我正在使用OpenLayers v3.6 (这很重要,因为我发现并且可能有效的大多数解决方案都适用于OpenLayers 2)。

I have a table and when I select a row in that table, I would like to highlight/select a corresponding feature on the OpenLayers map. 我有一个表,当我在该表中选择一行时,我想在OpenLayers地图上突出显示/选择相应的功能。 All features are simple polygons ( ol.geom.Polygon ) in the same vector layer ( ol.layer.Vector ). 所有要素都是同一矢量图层中的简单多边形( ol.geom.Polygon )( ol.layer.Vector )。

I set up select interaction like this: 我设置了这样的选择交互:

// 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;
}

This functions well when I want to select polygon by clicking on it on the map. 当我想通过在地图上单击多边形来选择多边形时,此功能很好。 But what I want is to achieve the same, not by clicking on map but rather click on some element outside the map (table row in my example, but it could be anything really). 但我想要的是实现同样的目标,而不是点击地图,而是点击地图外的一些元素(我的例子中的表格行,但它可能是真的)。

I would like to use select interaction because of event that is emitted and because of the styling it applies to selected features. 我想使用选择交互,因为发出的事件以及它应用于所选功能的样式。 However, if by any chance I can just manipulate the selected features in select interaction without having the same event it would be ok. 但是,如果有任何机会我可以在没有相同事件的情况下操纵选择交互中的所选特征,那就没问题。

I'm aware of this question & answer - Openlayers 3: Select a feature programmatically - but the problem is that I cannot ask in comments for clarification (for example, what exactly is mySelectControl ), because I don't have any reputation :) 我知道这个问题和答案 - Openlayers 3:以编程方式选择一个功能 - 但问题是我不能在评论中要求澄清(例如,究竟是什么mySelectControl ),因为我没有任何声誉:)

The way to do is in the linked question. 要做的是在相关问题中。 So, push a ol.Feature into the selected collection: 因此,将ol.Featureol.Feature选定的集合中:

var select = new ol.interaction.Select({
    //some options
});
map.addInteraction(select);

var selected_collection = select.getFeatures();
selected_collection.push(featurePoint);

If you want to trigger the select event: 如果要触发select事件:

select.dispatchEvent('select');

// OR

select.dispatchEvent({
  type: 'select',
  selected: [featurePoly],
  deselected: []
});

See demo! 看看演示!

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

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