[GIS] OpenLayers SelectFeature – Manually set feature as selected/unselected

openlayers-2

In the OpenLayers SelectFeature, how can I manually set a feature as selected/unselected and have it reflect the appropriate color?

For instance, when my application loads, it sends back the existing select state which I want to set on the polygons. Thanks!

Best Answer

I noticed that there are two methods which can be used to select a feature manually.

  1. Method select(feature) in the SelectFeatures control.
  2. Method clickFeature(feature) in the SelectFeatures control.

Here's a fiddle demonstrating that feature in case anyone else is interested.

At one point, I wasn't able to get select to work. Setting selectFeature.allowSelection to true let me select it, but later I noticed that it worked without it so I'm not sure when setting that to true is required. If you run into any issues, you might want to try playing with that attribute. Looking into the source further should allow us to understand the purpose of that variable.