GUIDE4YOU/OL3 - 如何读取图层的自定义属性

GUIDE4YOU/OL3 - how to read custom attributes of a layer

如何读取图层的自定义属性?

例如,我添加了一个具有自定义属性 test 和值 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); }); }); });

亲切的问候,

山姆

这可以通过 layer.get('test') 实现。

您不会获得带有 getLayers() 的图层,因为这些图层嵌套在不同的图层组中。

方法 addFeatureLayer 将 return 引用图层。您可以保存并稍后使用。

或者您可以使用 recursiveForEach,它将递归遍历所有嵌套层。

您可以使用 map.getLayerGroup().recursiveForEach(function (layer) { ... }) 对地图中的所有层执行此操作,也可以使用 map.get('featureLayers').recursiveForEach(function (layer) { ... })

仅对 featureLayers 或 baseLayers 执行此操作

谢谢西蒙!!

工作代码:

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