Programmatically trigger a select event in open layers 4

2.8k Views Asked by At

Is there any chance to trigger a manual selection for an ol.feature in ol4? I saw the post here, but i have the same issue : the custom selection style is not applied. Any workarounds to get programmatically selection working as normal selection(within map)?

1

There are 1 best solutions below

1
sudhir shakya On

The problem I had to solve was - if an user clicked on a record in an external table, I had to display a popup for that feature. This is how I solved the problem.

  1. When drawing the map, I saved the layer source, the list of features in a global variable.
  2. When user clicks on the record in the table, I get the ID of the record.
  3. Get the feature - layerSource.getFeatureById (id)
  4. Draw the popup. (don't think you are interested in this step)

If you have to set the style, I think you will need to remove the old Vector layer from the map, and redraw the layer again. For the style, you will need to use a function that will return a different style for the selected feature.