This is a rather bizarre behavior when I use the indexof to find layer names that contain "Buffer".
So by running this:
array.forEach(this.map.graphicsLayerIds, function(layerId) { var layer = this.map.getLayer(layerId); console.log(layer.name); }, this);
I get this output:
I modified it to capture only the layer names containing "Buffer", so I run this:
array.forEach(this.map.graphicsLayerIds, function(layerId) { var layer = this.map.getLayer(layerId); if((layer.name).indexOf("Buffer") > -1){ console.log(layer.name); } }, this);
and I get this:
meaning that the layer is undefined!!!
Ideas?
Thank you.
Solved! Go to Solution.
Lefteris,
OK, now I see the issue. You have one or more layers that do not have a name specified in your map, so you need to check for the existence of the name property first:
array.forEach(this.map.graphicsLayerIds, function(layerId) { var layer = this.map.getLayer(layerId); if(layer.name && layer.name.indexOf("Buffer") > -1){ console.log(layer.name); } }, this);
Lefteris,
Try this
if(layer.name.toString().indexOf("Buffer") > -1){
Thank Robert.
But I got the same error:
array.forEach(this.map.graphicsLayerIds, function(layerId) { layer = this.map.getLayer(layerId); mylayer.push(layer); }, this); for(j=0;j<mylayer.length; j++){ if(layer.name.toString().indexOf("Buffer") > -1){ console.log(mylayer.name); } }
Lefteris,
OK, I was not paying close enough attention to your code.
array.forEach(this.map.graphicsLayerIds, function(layerId) { layer = this.map.getLayer(layerId); mylayer.push(layer); }, this); for(j=0;j<mylayer.length; j++){ if(mylayer.name.toString().indexOf("Buffer") > -1){ console.log(mylayer.name); } }
Sorry, the problem, persist.
I also have to apologize, that after my first posting I introduced the array mylayer to see if I needed to push the layer into an array.
So going back to the first posting, the problem persist either way. The question is that if console.log can show the layer name as a string without the indexOf, why do we need to try to convert it to a string per your suggestion?
THANK YOU for your time.
Lefteris,
OK, now I see the issue. You have one or more layers that do not have a name specified in your map, so you need to check for the existence of the name property first:
array.forEach(this.map.graphicsLayerIds, function(layerId) { var layer = this.map.getLayer(layerId); if(layer.name && layer.name.indexOf("Buffer") > -1){ console.log(layer.name); } }, this);