[GIS] Openlayers 4 unselect a feature

openlayers

How can I unselect a feature in custom hyperlink onclick?

Basically I have select interaction that changes style of a marker and after I click anywhere on the map, then feature is unselected (goes back to default style). This part works just fine.

Now I have hyperlink "button" which closes overlay and should also perform unselection for selected feature. Unfortunately I can't really find a way to do this.

I have tried to attach selected feature to close button and setStyle to null, but no avail. Also tried to set default style on the marker, but then marker won't change when clicked on it again.

Example can be found at
http://jsfiddle.net/c3o6fy7g/5/

Explanation: red marker should change to pink and overlay is supposed to appear. Clicking on "x" closes overlay and should do unselect on the feature, after which marker should change back to green.

"x" button functionality is given at line 18 aka "closer.onclick = function() {"

Best Answer

You can simply add the following line within your closer click event:

select.getFeatures().clear();

select.getFeatures() will return the currently selected feature(s), and clear() will remove them from the selection.