首先通过JSON.parse将GeoJSON从字符串解析出来,直接解析会报错所以需要解析为一个Record<string, string>对象。再通过递归遍历的方式对Record对象进行遍历,达到解析GeoJSON对象的目的。
import { JSON } from '@kit.ArkTS';
function geoJsonAnalysisHandler(geoJsonData: object) {
Object.keys(geoJsonData).forEach(key => {
if (key == 'coordinates') {
console.info(JSON.stringify(Array.from(geoJsonData['coordinates'] as string)));
} else if ((typeof geoJsonData[key]) === 'object') {
console.info(key, ':');
geoJsonAnalysisHandler(geoJsonData[key]);
} else {
console.info(key, geoJsonData[key]);
}
});
}
@Entry
@Component
struct GeoJsonAnalysis {
public geoJsonString: string = `
{ "type": "FeatureCollection",
"features": [
{ "type": "Feature",
"geometry": {"type": "Point", "coordinates": [102.0, 0.5]},
"properties": {"prop0": "value0"}
},
{ "type": "Feature",
"geometry": {
"type": "LineString",
"coordinates": [
[102.0, 0.0], [103.0, 1.0], [104.0, 0.0], [105.0, 1.0]
]
},
"properties": {
"prop0": "value0",
"prop1": 0.0
}
},
{ "type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [
[ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0],
[100.0, 1.0], [100.0, 0.0] ]
]
},
"properties": {
"prop0": "value0",
"prop1": {"this": "that"}
}
}
]
}`;
build() {
Column() {
Button('点击测试')
.onClick(() => {
let geoJsonData = JSON.parse(this.geoJsonString) as Record<string, string>;
geoJsonAnalysisHandler(geoJsonData);
});
}
.align(Alignment.Center)
.justifyContent(FlexAlign.Center)
.height('100%')
.width('100%');
}
}