[GIS] Show/hide features on layer in OpenLayers

featuresopenlayers

In previous versions of OpenLayers, features on a layer could be hidden by changing visibility to 'hidden' but that does not seem to work on OpenLayers 4. How can features be modified so that we can hide them or show them? Ultimately I want to be able to show or hide features based on an event on my map.

Best Answer

One approach is to change the features' style. In order to hide a feature, you can set its style to an empty style object:

myFeatureIWantToHide.setStyle(new ol.style.Style({}));

To show this feature again, simply set the feature's style to null:

myFeatureIWantToHide.setStyle(null);