Skip to content

Instantly share code, notes, and snippets.

@acanimal
Created December 31, 2013 15:50
Show Gist options
  • Save acanimal/8198685 to your computer and use it in GitHub Desktop.
Save acanimal/8198685 to your computer and use it in GitHub Desktop.
OpenLayers3, given an initial layer (a group or a single layer) finds recursively a layer with the specified key-value.
/**
* Finds recursively the layer with the specified key and value.
* @param {ol.layer.Base} layer
* @param {String} key
* @param {any} value
* @returns {ol.layer.Base}
*/
function findBy(layer, key, value) {
if (layer.get(key) === value) {
return layer;
}
// Find recursively if it is a group
if (layer.getLayers) {
var layers = layer.getLayers().getArray(),
len = layers.length, result;
for (var i = 0; i < len; i++) {
result = findBy(layers[i], key, value);
if (result) {
return result;
}
}
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment