Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

What is the exact API to get all features in a vector layer in OpenLayers? Suppose I have the "map" object, within which is a "Vector Layer": how can I obtain features in the layer?

share|improve this question
add comment

1 Answer

up vote 4 down vote accepted

As far as I can tell, it's not part of the vector layer api (http://dev.openlayers.org/apidocs/files/OpenLayers/Layer/Vector-js.html).

vector_layer.features should do the trick, though. This is an array maintained by the layer, which saves references to each geometry it contains.

I don't know if you use the Firefox add-on called Firebug, but among other things, it makes interrogating the internals of javascript objects a breeze.

share|improve this answer
    
Just a note, it is part of the API, so layer.features is the right to get at them. dev.openlayers.org/docs/files/OpenLayers/Layer/… –  Vadim Nov 10 '11 at 19:52
    
Yes, I also noted that. But it seems that the property features can only be obtained from OpenLayers.Layer.Vector, while the object map only has access to OpenLayer.Layer. The former is inherited from the latter. Then how can I get OpenLayer.Layer.Vector? –  ChanDon Nov 11 '11 at 8:16
    
if you use c#or java, you need to do a cast, but here you can use use javascript magic :) if (layer.features) { //do stuff } –  D_Guidi Nov 11 '11 at 8:51
    
It works! Thank you~ –  ChanDon Nov 13 '11 at 2:16
    
Since vector_layer.features is somewhat internal we ended up extending the Vector layer and override the add method to keep a reference to added features. –  Martin Algesten Nov 13 '11 at 10:03
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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