i want to load the coordinates of a point that is stored inside a vector layer and the vector layer is stored inside a layergroup.
I do load the vector layers dynamically to the layergroup "Runs". So i can't just do:
var source = layer.getSource();
source.forEachFeature(function(feature){
var coord = feature.getGeometry().getCoordinates();
// ...
});
At the moment it is totally unclear how to filter for the layer and then for the point by name to get the coordinates.
So what i have available is the layer name, layergroup name and the feature name. I tried different approaches like:
My layergroup is called 'runs' so to get the layers inside runs i did:
i tried also:
runs.getLayers().getSource();
But i get:
Uncaught TypeError: runs.getLayers(…).getSource is not a function.
I'm having a hard time to get my head around this.
So i made it a little furhter:
function getActiveLayer(layerName,picID) {
// get layerGroups
var layers = map.getLayers();
var length = layers.getLength(), l;
for (var i = 0; i < length; i++) {
l = layers.item(i);
var lt = l.get('title');
// check for layers within groups
if (lt === 'Runs') { // Title of Group
// get layers from Runs
var layers = l.getLayers();
//get length
var length = layers.getLength(), l;
if (length > 0) {
for (var i = 0; i < length; i++) {
l = layers.item(i);
var lt = l.get('title');
// check for Layer Title
if (lt === layerName) { // Title if Layer
var innerLayers = l.getSource().getFeatureById(picID).getGeometry().getCoordinates();
return innerLayers;
}
}
}
}
}
};
With this function i get the LayerGroup list that i can parse and identify the Group "Runs" i want. Until this point it is working.
The Problem starts at the second Point. The layers.getLength() is always zero for the layers that are inside the "Runs" Group.
The Upper one is the getLayers from map.
The lower one is the getLauers from "Runs".
Why is length 0 if there are Layers inside the array?
EDIT
function getActiveLayer(layerName,picID) {
var layers = map.getLayers();
console.log(layers);
var length = layers.getLength(), l;
console.log(length);
for (var i = 0; i < length; i++) {
l = layers.item(i);
var lt = l.get('title');
// check for layers within groups
if (lt === 'Runs') { // Title of Group
var layers = l.getLayers().getArray();
console.log(layers);
//if (length > 0) {
for (var i = 0; i < 99; i++) {
l = layers[i];
console.log(l);
console.log(layerName);
var lt = l.get('title');
console.log(lt);
// check for Layer Title
if (lt === layerName) { // Title if Layer
var innerLayers = l.getSource().getFeatureById(picID).getGeometry().getCoordinates();
return innerLayers;
}
}
}
}
};
This function works. The only Problem i have that i can not figure out how the get the length of the object to config the second for loop.
Best Answer
Just in case someone else is starting with openlayes. This is a working function i came up with:
The function searches for a specified feature inside a specified layer inside a specified layer group and returns the features coordinates.