若要将经纬度数据生成如下格式geojson数据并用前端解析(格式参照:网址:geojson.io)
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
114.10125732421875,
27.249746156836583
],
[
113.74420166015624,
26.79465448763808
],
[
114.169921875,
26.41647024087778
],
[
114.46929931640624,
26.792202785452883
],
[
114.38690185546875,
27.17891247557853
],
[
114.21661376953125,
27.264395776495334
],
[
114.10125732421875,
27.249746156836583
]
],
[
[
113.03558349609374,
27.23021032948516
],
[
113.01910400390624,
26.941659545381516
],
[
113.2415771484375,
26.8730809659384
],
[
113.24981689453125,
27.42297612892041
],
[
113.08502197265625,
27.47172437423024
],
[
113.03558349609374,
27.23021032948516
]
]
]
}
},
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
113.03558349609374,
27.23021032948516
],
[
113.01910400390624,
26.941659545381516
],
[
113.2415771484375,
26.8730809659384
],
[
113.24981689453125,
27.42297612892041
],
[
113.08502197265625,
27.47172437423024
],
[
113.03558349609374,
27.23021032948516
]
]
]
}
},
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
112.994384765625,
26.716173757934094
],
[
113.00811767578125,
26.49024045886963
],
[
113.42559814453125,
26.382027976025352
],
[
113.48052978515625,
26.664641348582713
],
[
113.291015625,
26.782395446979752
],
[
112.994384765625,
26.716173757934094
]
]
]
}
}
]
}
使用C++生成(使用cJSON库):
//myJson为最高层,含有"type": "FeatureCollection","features": [{},{},{}……]
cJSON* myJson = cJSON_CreateObject();
cJSON_AddItemToObject(myJson, "type", cJSON_CreateString("FeatureCollection"));
cJSON* featuresArry;
featuresArry = cJSON_CreateArray(); /*创建数组,features为feature数组,feature含有"type": "Feature","properties": {},"geometry" : {[[],[],[]……]}*/
cJSON_AddItemToObject(myJson, "features", featuresArry);
cJSON* feature = cJSON_CreateObject();
cJSON_AddItemToArray(featuresArry, feature);
cJSON_AddItemToObject(feature, "type", cJSON_CreateString("Feature"));
cJSON_AddItemToObject(feature, "properties", cJSON_CreateString(""));
//"geometry" : {[[],[],[]……]}
cJSON* geometry = cJSON_CreateObject();
cJSON_AddItemToObject(feature, "geometry", geometry);
cJSON_AddItemToObject(geometry, "type", cJSON_CreateString("Polygon"));
cJSON* coordArry = cJSON_CreateArray(); /*创建数组*/
cJSON_AddItemToObject(geometry, "coordinates", coordArry);
cJSON* PolygonArry = cJSON_CreateArray(); /*创建数组*/
cJSON_AddItemToArray(coordArry, PolygonArry);
cJSON* PointArry = cJSON_CreateArray(); /*创建数组*/
cJSON_AddItemToArray(PolygonArry, PointArry);
cJSON_AddItemToArray(PointArry, cJSON_CreateNumber(L[(jj - 1) * xsize + ii - 1]));
cJSON_AddItemToArray(PointArry, cJSON_CreateNumber(B[(jj - 1) * xsize + ii - 1]));
使用Js解析:
const url = require('../../assets/data/resJSON.json');
var mypolygonArr1= []
console.log(url);
// let obj=JSON.parse(url);
console.log(url.features);
for (const feature in url.features) {
// console.log(url.features[feature].geometry.coordinates);
var mycoordinates=url.features[feature].geometry.coordinates;//获取到了坐标数组
for (const index in mycoordinates) {
var map = new Map();
var myxy=mycoordinates[index];//
var list = [];
for (const index2 in myxy) {
var L=myxy[index2][0]
var B=myxy[index2][1]
list.push({lng:L,lat:B})
}
// console.log(list);
mypolygonArr1.push(list)
}
}