0

How can I read custom attributes of a layer?

For example I've added a layer with custom attribute test and value sam.

Console log map layers

createG4U('#g4u-map', 'conf/client.commented.json', 'conf/layers.commented.json').then(function (map) { map.asSoonAs('ready', true, function () { map.get('api').addFeatureLayer({ "id": "1", "type": "GeoJSON", "style": stylefunction, "source": { "url": "files/sample.json" }, "visible": true, "test" : "sam" }); map.getLayers().forEach(function (layer) { console.log(layer); }); }); });

Kind regards,

Sam

Sam
  • 23
  • 4

2 Answers2

0

This is possible with layer.get('test').

you won't get the layer with getLayers(), as the layers are nested in different layergroups.

the method addFeatureLayer will return a reference to the layer. You can save it and use it later.

Or you can use recursiveForEach which will iterate recursively through all nested layers.

You can either do it for all layers in the map with map.getLayerGroup().recursiveForEach(function (layer) { ... }) or you can do it for featureLayers or baseLayers only with map.get('featureLayers').recursiveForEach(function (layer) { ... })

Simon Zyx
  • 6,503
  • 1
  • 25
  • 37
  • Not working, I get always 'undefined'. Even reading out attribute id (mandatory attribute) is 'undefined'. – Sam May 12 '17 at 10:50
0

Thanks Simon!!

Working code:

createG4U('#g4u-map', 'conf/client.commented.json', 'conf/layers.commented.json').then(function (map) { map.asSoonAs('ready', true, function () { map.get('api').addFeatureLayer({ "id": "1", "type": "GeoJSON", "style": stylefunction, "source": { "url": "files/sample.json" }, "visible": true, "test" : "sam" }); map.get('featureLayers').recursiveForEach(function (layer) { console.log(layer.get('test')); }); //Make the layer visible in the menu map.get('UIConfigurator').configureUI(); }); });

Sam
  • 23
  • 4