map.getLayers().forEach(function (elem) {//图层组
if (elem instanceof ol.layer.Group) {
//添加父节点
groupCount++;
zTree.addNodes(null,
{
id: groupCount,
isParent: true,
name: "图层组" + groupCount,
});
elem.getLayers().forEach(function (lay) {//图层
//添加子节点
layerCount++;
treeNode = zTree.addNodes(
zTree.getNodeByParam("id", groupCount , null),//关键点!!!!
{
id: groupCount * 10 + layerCount,
pId: 2,
isParent: false,
name: lay.get('title')
});
})
} else {
//添加父节点
var layerName = elem.get('title');//根图层
}
});