1

I wonder if there is a way to check whether a pointfeature (OpenLayers.Feature.Vector) is selected. I read the API, but didn't find any solution. Any idea?

4

After a feature is selected, it's added to the vector layer's selectedFeatures array (and removed after it's deselected)


You can test it in your browser: http://openlayers.org/en/latest/examples/select-features.html

  • Draw a polygon
  • Open JS console (i.e. Firebug or Chrome dev tools)
  • Type:
var vectorLayer = map.getLayersByName("Vector Layer")[0];
var feature = vectorLayer.features[0];
vectorLayer.selectedFeatures.indexOf(feature);

=>  -1 // (i.e. not selected)
  • Select the polygon and check again:
vectorLayer.selectedFeatures.indexOf(feature)

=>  0 // (i.e. the polygon is selected)
1

For newer Openlayers 3 the array is not kept in the Layer any longer. You can get by the interaction.select itself:

//get features as collection
var features=interaction_select.getFeatures();
//get length (.length doesnot work in my case)
var int_features=features.getLength();
//or access by numeric index like any collection
var first_item=features.item(0);

If you want to read more about interaction or the collection then just begin there: http://openlayers.org/en/v3.7.0/apidoc/ol.interaction.Select.html#getFeatures

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.