0

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. But var length = layers.getLength always returns 0.

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;
          }
        }
      }
    }
  }


};

getLayers from map and LayerGroup

The Upper one is the getLayers from map. The lower one is the getLauers from "Runs".

Can somebody explain why length is actual 0?l

Edit:

I updated the code:

 function getActiveLayer(layerName, picID) {
  // get layerGroups
  var layers = map.getLayers();
  var length = layers.getLength();
  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().getArray();
      //get length
      var length2 = layers.getLength();
      console.log(length2);
      if (length > 0) {
        for (var i = 0; i < length2; 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;
          }
        }
      }
    }
  }
};

console.log(length2); is still 0.

zwnk
  • 101
  • 3

2 Answers2

0

var length = layers.getLength(), l;

This is horrible code and you most likely did not do it on purpose. Also see: comma separator At this position, you probably just wanted to get length from l:

var length = l.getLength();

You built a particularly nasty snippet. Run the code below. You probably expect it to go 3 time throught the loop - it does not.

There is no block-level-scope in javascript, only function-level-scope, so var x=y inside a for does not define the variable only for the for block but for the whole function.

Look at variable shadowing questions, which explain the issue in detail. Use unique variable names. Never try to do variable shadowing, it's really bad practice!

function badpractice(number) {

  var length = number;
  console.log('length before loop', length);
  for(var i=0;i < length;i++){
    console.log('length before for assign', length);
    var length = i;
    console.log('length after for assign', length);
  }
  console.log('length after loop', length);
};

badpractice(3);
dube
  • 4,898
  • 2
  • 23
  • 41
  • HI, Thank you for your advice! I appreciate it alaways if someone takes the time to point out an error. But my problem is not solved. the length of "Runs" is still 0. – zwnk Feb 06 '18 at 12:05
  • append your updated code to the question so we can check it – dube Feb 06 '18 at 12:59
0

i found the solution. here you go:

   function getActiveLayer(layerName,picID) {
        var layers = map.getLayers();
            var length = layers.getLength();
            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();
                  var length = l.getLayers().getArray().length;
                  // loop over layers
                    for (var i = 0; i < length; i++) {
                      l = layers[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; // return coord of feature
                      }
                    }
                }
            }


      };
zwnk
  • 101
  • 3