Openlayers 3: function selection programmatically

I am trying to upgrade my system from Openlayers 2 to Openlayers 3, and I have one specific problem that I cannot understand.

My application has a grid and a map, and when the user clicks on the grid, I want to select the corresponding point on the map.

In Openlayers 2, I used the following:

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

I cannot find or understand how to do the same in Openlayers 3.

Therefore, to be clear, I have a function that I found programmatically, and I want to select this function on the map (programmatically)!

I cannot find anything in the API, but it may be due to my lack of understanding, as I am new to Openlayers.

+6
source share
3 answers

To do this, you need to do the following:

 mySelectControl.getFeatures().clear() -> removes the selected items mySelectControl.getFeatures().push(featureToSelect) -> selects the applied feature 
+10
source
  • Add the selected interaction to your map.

     var selectInteraction = new ol.interaction.Select(); map.addInteraction(selectInteraction); 
  • Add all the functions you want to select in the array of interaction selection functions.

     selectInteractions.getFeatures().push(featureToSelect); 
+1
source
 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 char on latest openlayers 4.5

0
source

Source: https://habr.com/ru/post/976863/


All Articles