递归遍历
2020-04-29 本文已影响0人
宿州刘德华
/**
* 递归查找树状json数据
* @leafId 查找的id,
@nodes 原始Json数据
@path 供递归使用
*/
/* findPathById(nodeId, nodes, path) {
if (path === undefined) {
path = [];
}
for (let i = 0; i < nodes.length; i++) {
let tmpPath = path.concat();
tmpPath.push(nodes[i]);
if (nodeId == nodes[i].id) {
return tmpPath;
}
if (nodes[i].children) {
let findResult = this.findPathById(
nodeId,
nodes[i].children,
tmpPath
);
if (findResult) {
return findResult;
}
}
}
},
*/
/* ergodicJsonTree2(node, results) {
if (results === undefined) {
results = [];
}
if (node instanceof Array) {
node.forEach((item, index) => {
if (!item.children || item.children.length === 0) {
results.push(item);
} else {
this.ergodicJsonTree2(item.children, results);
}
});
} else {
if (!node.children || node.children.length === 0) {
results.push(node);
} else {
this.ergodicJsonTree2(node.children, results);
}
}
return results;
},*/
/***
* 遍历jsonTree 加载vectorLayer
* */
/* ergodicJsonTree(json) {
json.forEach((item, index) => {
if (!item.children || item.children.length === 0) {
//为每个图层创建一个layer
let vectorSource = new ol.source.Vector();
let vectorLayer = new ol.layer.Vector({
source: vectorSource,
isGroup: true
});
let nodes = this.findPathById(item.id, this.setTree);
let rgb = {r: 0, g: 0, b: 0};
if (nodes && nodes instanceof Array && nodes.length >= 2) {
let rgba = nodes[1].rgba.toString().split(",");
rgb.r = parseInt(rgba[0]);
rgb.g = parseInt(rgba[1]);
rgb.b = parseInt(rgba[2]);
}
if (
item.pointAndLine &&
(item.pointAndLine.point || item.pointAndLine.line)
) {
let points = item.pointAndLine.point;
let lines = item.pointAndLine.line;
if (points.length > 0 || lines.length > 0) {
if (points instanceof Array && points.length > 0) {
points.forEach((item_P, index) => {
if (
this.isNumber(item_P.geometry.xCoordinate) &&
this.isNumber(item_P.geometry.yCoordinate)
) {
let f_feature = new ol.Feature(
new ol.geom.Point([
item_P.geometry.xCoordinate,
item_P.geometry.yCoordinate
])
);
f_feature.setStyle(
this.getPointStyleByImg(
"http://www.wshunvx.cn/link/png" + item.source
)
);
f_feature.setProperties(item_P.attributes);
vectorSource.addFeature(f_feature);
}
});
}
if (lines instanceof Array && lines.length > 0) {
lines.forEach((item_L, index) => {
if (
this.isNumber(item_L.geometry.qd.xCoordinate) &&
this.isNumber(item_L.geometry.qd.yCoordinate) &&
this.isNumber(item_L.geometry.zd.xCoordinate) &&
this.isNumber(item_L.geometry.zd.yCoordinate)
) {
let lineStringFeature = new ol.Feature(
new ol.geom.LineString([
[
item_L.geometry.qd.xCoordinate,
item_L.geometry.qd.yCoordinate
],
[
item_L.geometry.zd.xCoordinate,
item_L.geometry.zd.yCoordinate
]
])
);
lineStringFeature.setProperties(item_L.attributes);
lineStringFeature.setStyle(this.getLineStringStyle(rgb));
vectorSource.addFeature(lineStringFeature);
}
});
}
}
}
let ids = "";
nodes.forEach((nodeItem, nodeIndex) => {
ids += nodeItem.id + "_";
});
vectorLayer.set("ids", ids, false);
vectorLayer.setVisible(false);
map.addLayer(vectorLayer);
} else {
this.ergodicJsonTree(item.children);
}
});
},*/