Openlayers 3: Select a feature programmatically

前端 未结 3 1107
梦毁少年i
梦毁少年i 2021-02-05 11:59

I am trying to upgrade my system from Openlayers 2 to Openlayers 3 and I have having one particular issue that I cannot seem to figure out.

My application has a grid and

相关标签:
3条回答
  • 2021-02-05 12:14
    var selectInteraction = new ol.interaction.Select(}); 
    map.addInteraction(selectInteraction);
    
    function highlightFeature(feat){
       selectInteraction.getFeatures().push(feat);
       selectInteraction.dispatchEvent({
          type: 'select',
          selected: [feat],
          deselected: []
       });
    }
    

    works like a char on latest openlayers 4.5

    0 讨论(0)
  • 2021-02-05 12:15

    To do this you need to do the following:

    mySelectControl.getFeatures().clear() -> removes the selected items
    
    mySelectControl.getFeatures().push(featureToSelect) -> selects the applied feature
    
    0 讨论(0)
  • 2021-02-05 12:25
    1. Add a select interaction to your map.

      var selectInteraction = new ol.interaction.Select();
      map.addInteraction(selectInteraction);
      
    2. Add any features you want to select to the select interaction's feature array.

      selectInteractions.getFeatures().push(featureToSelect);
      
    0 讨论(0)
提交回复
热议问题