3

I need to select link using link properties, I am able to find the feature but not able to highlight programmatically. please help..

In openlayers 2 we have selectFeature but not sure what is in ol3

2 Answers 2

6

@ahocevar's answer saved me hours but one thing is missing from the code is to add select interaction to map object:

var select = new ol.interaction.Select();
//make sure you add select interaction to map
map.addInteraction(select);
var features = select.getFeatures();
// now you have an ol.Collection of features that you can add features to
features.push(feature);
// now the pushed feature is highlighted

//to dehighlight, just simply remove the feature from select
features.remove(feature);
2
5

Programmatic feature selection works by manipulating the Select interaction's feature collection:

var select = new ol.interaction.Select();
var features = select.getFeatures();
// now you have an ol.Collection of features that you can add features to
features.push(feature);
// now the pushed feature is highlighted

The above snippet assumes that the feature you want to highlight is assigned to the feature variable.

2
  • I am trying this answer and the following, but I cannot get the feature highlighted. the feature is found by another function and then added to a point layer with a specific style. This point is not added to the map until the function is over, hence the highlight is not also happening in the middle of processing. What I am missing?
    – msc87
    Nov 11, 2015 at 12:07
  • could you please check my question as well: gis.stackexchange.com/questions/169872/…
    – msc87
    Nov 12, 2015 at 8:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.