Openlayers 3: Select a feature programmatically

Scott Sellers picture Scott Sellers · Oct 17, 2014 · Viewed 12.7k times · Source

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 a map and when a user clicks on the grid I want to select the relevant 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.

So to be clear, I have a feature which I have found programmatically and I want to select that feature on a map (programmatically)!

I cannot seem to find anything in the APIs but that might be due to my lack of understanding as I am new to Openlayers.

Answer

Scott Sellers picture Scott Sellers · Oct 17, 2014

To do this you need to do the following:

mySelectControl.getFeatures().clear() -> removes the selected items

mySelectControl.getFeatures().push(featureToSelect) -> selects the applied feature