echart map 编辑 和 压缩
geojson在线编辑器:
https://mapshaper.org/
echartsmap压缩说明:https://blog.csdn.net/isea533/article/details/79194819
压缩方法:javascript版本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | // 压缩文件=============================================== function convert2Echarts(rawStr,fileName,type){ var results= "" ; var json = JSON.parse(rawStr); // Meta tag json.UTF8Encoding = true ; var features = json.features; if (!features) { return ; } features.forEach( function (feature){ var encodeOffsets = feature.geometry.encodeOffsets = []; var coordinates = feature.geometry.coordinates; if (feature.geometry.type === 'Polygon' ) { coordinates.forEach( function (coordinate, idx){ coordinates[idx] = encodePolygon( coordinate, encodeOffsets[idx] = [] ); }); } else if (feature.geometry.type === 'MultiPolygon' ) { coordinates.forEach( function (polygon, idx1){ encodeOffsets[idx1] = []; polygon.forEach( function (coordinate, idx2) { coordinates[idx1][idx2] = encodePolygon( coordinate, encodeOffsets[idx1][idx2] = [] ); }); }); } }); if (type=== 'json' ){ results=JSON.stringify(json); } else { results=addEchartsJsWrapper(JSON.stringify(json), fileName); } return results; }; function encodePolygon(coordinate, encodeOffsets) { var result = '' ; var prevX = quantize(coordinate[0][0]); var prevY = quantize(coordinate[0][1]); // Store the origin offset encodeOffsets[0] = prevX; encodeOffsets[1] = prevY; for ( var i = 0; i < coordinate.length; i++) { var point = coordinate[i]; result+=encode(point[0], prevX); result+=encode(point[1], prevY); prevX = quantize(point[0]); prevY = quantize(point[1]); } return result; } function addAMDWrapper(jsonStr) { return [ 'define(function() {' , ' return ' + jsonStr + ';' , '});' ].join( '\n' ); } function addEchartsJsWrapper(jsonStr,fileName) { return [ '(function (root, factory) {' , " if (typeof define === 'function' && define.amd) {" , " define(['exports', 'echarts'], factory);" , " } else if (typeof exports === 'object' && typeof exports.nodeName !== 'string') {" , " factory(exports, require('echarts'));" , " } else {" , " factory({}, root.echarts);" , " }" , " }(this, function (exports, echarts) {" , " var log = function (msg) {" , " if (typeof console !== 'undefined') {" , " console && console.error && console.error(msg);" , " }" , " }" , " if (!echarts) {" , " log('ECharts is not Loaded');" , " return;" , " }" , " if (!echarts.registerMap) {" , " log('ECharts Map is not loaded')" , " return;" , " }" , " echarts.registerMap('" +fileName+ "'," + jsonStr, ' )}));' ].join( '\n' ); } function encode(val, prev){ // Quantization val = quantize(val); // var tmp = val; // Delta val = val - prev; if (((val << 1) ^ (val >> 15)) + 64 === 8232) { //WTF, 8232 will get syntax error in js code val--; } // ZigZag val = (val << 1) ^ (val >> 15); // add offset and get unicode return String.fromCharCode(val+64); // var tmp = {'tmp' : str}; // try{ // eval("(" + JSON.stringify(tmp) + ")"); // }catch(e) { // console.log(val + 64); // } } function quantize(val) { return Math.ceil(val * 1024); } |
压缩方法:
var _jsons=convert2Echarts(JSON.stringify(_SL.mapjson),'中国地图华北冀北','json');
console.log(_jsons);
解压缩:
在echart地图加载完成后,将整个mapjson stringify()后输出来,就是解压后的文件。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步