For another project I need to compare the feature attributes from a GeoJSON layer with a given user input.
As a first step I created a basic map based on the OpenLayers Example and simply tried to get all features from the GeoJSON layer as an array.
//def vectorLayer
const vectorLayer = new ol.layer.Vector({
source: new ol.source.Vector({
url: 'https://openlayers.org/data/vector/ecoregions.json',
format: new ol.format.GeoJSON(),
}),
style: new ol.style.Style({
stroke: new ol.style.Stroke({
color: '#501020',
width: 1
})
})
});
//def map
const map = new ol.Map({
layers: [vectorLayer],
target: 'map',
view: new ol.View({
center: [0, 0],
zoom: 2,
}),
});
//get simple array with all features
var vectorSource = vectorLayer.getSource();
console.log(vectorSource.getFeatures());
The source.getFeatures()
method should return all features from the source as an array.
In my case the result is an empty array [].
So can I access the features from the vectorLayer? I prepared a working jsfiddle for better understanding.