How to get features from vector layer in Openlayers 3
Asked Answered
G

2

19

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?

Gratiana answered 30/3, 2015 at 16:58 Comment(0)
P
35

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

Pelmas answered 31/3, 2015 at 9:4 Comment(0)
D
0
this.vectorLayer = new VectorLayer({
  source: new VectorSource({
    url:'https://json.extendsclass.com/bin/d4f21e1dd8ed',
    format: new GeoJSON()
  }),
});
this.maps.addLayer(this.vectorLayer)

var source = this.vectorLayer.getSource();
var features = source.getFeatures();
console.log(features,'FeatureData');

When we getting features from source then its returning 0 array you can see in image click here for image and in my url one feature available.

Dichromate answered 3/2, 2023 at 11:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.