报错是因为你的geoJson数据有echarts不认识的类型,比如GeometryCollection,
那么通过turf将它转换为echarts认识的类型不就好了
import * as echarts from 'echarts'
import * as turf from "@turf/turf";
let yishuixian = require('../../../public/json/yishuixian.json')
export const znMap = {
yishuixian: "沂水县"
};
export function registerMap(mapName, geoJson) {
if (mapName && geoJson) {
let map = eval(geoJson);
map = toSplit(map);
if (Object.keys(znMap).includes(geoJson)) {
echarts.registerMap(mapName, map);
} else {
echarts.registerMap(mapName, yishuixian)
}
}
}
function toSplit(map) {
let tempArr = map.features.map(i => {
if (i.geometry.type != "GeometryCollection") {
return i;
} else {
return transFormGeoJson(i);
}
})
return turf.featureCollection(tempArr);;
}
function transFormGeoJson(map) {
// 将GeometryCollection类型转换为MultiPolygon类型
const multiPolygon = turf.multiPolygon(
map.geometry.geometries.map((geometry) => {
return geometry.coordinates;
})
);
multiPolygon.properties = map.properties;
return multiPolygon;
}