背景
專案中用echarts繪制地圖,由于網上鄉鎮級別行政邊界地圖賊少,所以需要用到bigMap+geojson.io去繪制自定義地圖,詳情請看解決如何整理出鄉鎮級的地圖json,以此使用echarts繪制出鄉鎮級的資料
但是由于生成的地圖里有一個區域為兩塊不連續的地圖塊,所以生成的geoJson中此區域的geometry.type===GeometryCollection.
然而 echarts 中對于此型別沒有做處理,詳情見原始碼
echarts\lib\coord\geo\parseGeoJson.js
第133行左右

這段代碼的意思是決議geoJson
解決方法
需要修改如下函式:
echarts 源代碼 大約121行
function _default(geoJson, nameProperty) {}
直接復制粘貼:
function _default(geoJson, nameProperty) {
decode(geoJson);
return zrUtil.map(
zrUtil.filter(geoJson.features, function(featureObj) {
if (featureObj.geometry.geometries) {
let geometry = featureObj.geometry.geometries.map(i => {
return i.coordinates;
});
let { type, properties, ...params } = featureObj;
return { type, properties, geometry };
}
// Output of mapshaper may have geometry null
return (
featureObj.geometry &&
featureObj.properties &&
featureObj.geometry.coordinates &&
featureObj.geometry.coordinates.length > 0
);
}),
function(featureObj) {
var properties = featureObj.properties;
var geo = featureObj.geometry;
var coordinates = geo.coordinates;
var geometries = [];
if (geo.type === "GeometryCollection") {
let geometry = {
type: "Polygon"
};
let coordinatesArr = featureObj.geometry.geometries.map(i => {
return i.coordinates;
});
geometry.coordinates = coordinatesArr;
console.log(coordinatesArr, "coordinatesArr");
coordinatesArr.forEach(i => {
geometries.push({
type: "polygon",
// According to the GeoJSON specification.
// First must be exterior, and the rest are all interior(holes).
exterior: i[0],
interiors: i.slice(1)
});
});
}
if (geo.type === "Polygon") {
console.log("coordinatesPolygon", coordinates);
geometries.push({
type: "polygon",
// According to the GeoJSON specification.
// First must be exterior, and the rest are all interior(holes).
exterior: coordinates[0],
interiors: coordinates.slice(1)
});
}
if (geo.type === "MultiPolygon") {
zrUtil.each(coordinates, function(item) {
if (item[0]) {
geometries.push({
type: "polygon",
exterior: item[0],
interiors: item.slice(1)
});
}
});
}
console.log(
properties[nameProperty || "name"],
geometries,
properties.cp,
"asdfasdfasdf"
);
var region = new Region(
properties[nameProperty || "name"],
geometries,
properties.cp
);
region.properties = properties;
return region;
}
);
}
ps: 去git上看echarts之前的issues,有類似問題,修改前可以去該issues參觀一下
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/265369.html
標籤:其他
