vectoropenlayers-3geojson

How to get features from vector layer in Openlayers 3


I am trying to get the features from my vector layer. The vector layer is composed from a GeoJSON document loaded via Geoserver. I tried vector.features but in vain. Could anyone help with this?


Solution

  • The architecture of OL3 distinguishes between a layer and their source. So to get access to the features of a layer you first have to access the source of the layer. This is done via:

    var source = layer.getSource();
    

    In case of a vector layer you will than get a ol.source.Vector object. From this object you can access your features via:

    var features = source.getFeatures();
    

    Further you got the possibility to access special features via getFeatureById(id) or getFeaturesAtCoordinate(coordinate). For more information see the api documentation http://openlayers.org/en/v3.4.0/apidoc/ol.source.Vector.html