首页 前端知识 解决echarts地图geoJson报错问题(Invalid geoJson format Cannot read prope)改源码就太蠢了

解决echarts地图geoJson报错问题(Invalid geoJson format Cannot read prope)改源码就太蠢了

2024-02-25 11:02:37 前端知识 前端哥 547 169 我要收藏

报错是因为你的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;
}

转载请注明出处或者链接地址:https://www.qianduange.cn//article/2687.html
标签
评论
会员中心 联系我 留言建议 回顶部
复制成功!