簡體   English   中英

Openlayers 3:如何使用ol.interaction.Select以編程方式選擇功能?

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

我正在使用OpenLayers v3.6 (這很重要,因為我發現並且可能有效的大多數解決方案都適用於OpenLayers 2)。

我有一個表,當我在該表中選擇一行時,我想在OpenLayers地圖上突出顯示/選擇相應的功能。 ) in the same vector layer ( ). 所有要素都是同一矢量圖層中的簡單多邊形( )( )。

我設置了這樣的選擇交互:

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

當我想通過在地圖上單擊多邊形來選擇多邊形時,此功能很好。 但我想要的是實現同樣的目標,而不是點擊地圖,而是點擊地圖外的一些元素(我的例子中的表格行,但它可能是真的)。

我想使用選擇交互,因為發出的事件以及它應用於所選功能的樣式。 但是,如果有任何機會我可以在沒有相同事件的情況下操縱選擇交互中的所選特征,那就沒問題。

), because I don't have any reputation :) 我知道這個問題和答案 - Openlayers 3:以編程方式選擇一個功能 - 但問題是我不能在評論中要求澄清(例如,究竟是什么 ),因為我沒有任何聲譽:)

要做的是在相關問題中。 因此,將ol.Featureol.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: []
});

看看演示!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM