echarts 加载乡镇地图

echarts是一款Apache基金会下孵化项目之一,是一个使用 JavaScript 实现的开源可视化库,可以流畅的运行在 PC 和移动设备上,兼容当前绝大部分浏览器(IE8/9/10/11,Chrome,Firefox,Safari等),底层依赖矢量图形库 ZRender,提供直观,交互丰富,可高度个性化定制的数据可视化图表。

echarts支持坐标系地图,使用geojson的格式进行加载,最终通过canvas进行绘制。使用echarts加载乡镇地图也是十分简单,效果也非常好,同时支持多种动画、渲染方式。

一、获取地图数据

乡镇级别的数据可以通过水经注软件进行下载。如图所示,下载需要的矢量数据,这里记得选择坐标系为WGS84 经纬度投影坐标系
水经注下载地图

下载后目录结构如下
地图数据

二、转换为geojson

使用mapshaper工具对shp文件进行转换到处为geojson文件。
点击select打开选择文件框,选择刚刚下载的乡镇shapefile文件。
mapshaper
选中刚刚下载的文件,所有文件都要选中
选中下载的地图数据
点击import,导入数据
导入数据展示
成功导入,显示下载的乡镇数据。点击右上角的export按钮进行导出
导出按钮
选择geojson导出
导出后便得到了需要的geojson文件了。
导出后还需要做一步处理,导出的文件的属性是大写的Name,如下图。这样不会被echarts识别,echarts会区分大小写,这样加载会显示不了名称。
Name
对json的所有Name替换为name后保存即可。

三、echarts使用

使用echarts对地图加载

$.get(this.mapJson, (geoJson) => {
    // 基于准备好的dom,初始化echarts实例
    echarts.registerMap('nh_town', geoJson);
    this.mapChart = echarts.init(document.getElementById(this.chartId));
    //将json请求的数据的内容封装为echarts显示的内容
    this.mapChartData = geoJson.features.map((feature, index) => {
      var rObj = {};
      rObj.name = feature.properties.name;
      rObj.value = this.effectSymbolCor[feature.properties.name];  //是每个镇中心的经纬度
      return rObj;
    });
    this.mapOption.series = this.initMapChartSeries(this.mapChartData);

    this.mapChart.setOption(this.mapOption);

    //点击
    this.mapChart.on('click', function (params) {
      console.log(params)
      var _self = this;

    });
  })

最终效果图:
效果图

  • 5
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
function getGzMap(_data) { if (_chinaMap == undefined) { var dom = document.getElementById("container"); _chinaMap = echarts.init(dom); _chinaMap.on('click', function(params) { console.log(params); var _type = params.seriesType; if (_type == "map") { //window.parent.aaa('aa') //调用父页面方法 } else if (_type == "effectScatter") { window.parent.showMap(); } }) } var option = { backgroundColor: 'rgba(0,0,0,0)', visualMap: { type: 'piecewise', show: false, min: 0, max: 300, splitNumber: 3, itemWidth: 10, itemHeight: 10, itemGap: 5, seriesIndex: [1], pieces: [ { min: 0, max: 100, label: '优' }, { min: 101, max: 200, label: '良' }, { min: 201, max: 300, label: '高风险' } ], //color: ['#FA4D08', '#4BD94F', '#FBD32B'], //红、绿、黄 color: ['#F8DAE6', '#FEF9B5', '#B0D8B3'], //红、黄、绿 textStyle: { color: '#9EA8B1', fontSize: 10 } }, tooltip: { formatter: '{b}' }, geo: { map: 'guangdong', roam: true, aspectScale: 1, zoom: 1.5, layoutCenter: ['55%', '40%'], layoutSize: 500, label: { normal: { show: true }, emphasis: { show: true } }, itemStyle: { normal: { areaColor: '#323c48', borderColor: '#111', borderColor: '#3BB4DF', shadowColor: '#25A3FC', shadowBlur: 10 }, emphasis: { areaColor: '#ddb926' } } }, series: [{ type: 'effectScatter', coordinateSystem: 'geo', data: unitData, symbolSize: 10, symbol: 'image://../../../../Content/images/One/fire.png', //symbolRotate: 35, rippleEffect: { period: 4, scale: 5, brushType: 'fill', }, label: { normal: { formatter: '{b}', position: 'right', show: false }, emphasis: { show: false } }, itemStyle: { normal: { color: '#fff' } } }, { name: '', type: 'map', geoIndex: 0, mapType: 'guangdong', // 自定义扩展图表类型 label: { normal: { show: true, } }, itemStyle: { normal: { label: { show: true, fontSize: 10, color: '#111' }, shadowColor: '#ddb926', shadowBlur: 5, }, emphasis: { label: { show: true }, shadowColor: 'rgba(0, 0, 0, 0.5)', shadowBlur: 10 } }, data: _data }, { type: 'effectScatter', coordinateSystem: 'geo', data: windData, symbolSize: 10, symbol: 'image://../../../../Content/images/One/wind.png', //symbolRotate: 35, rippleEffect: { period: 4, scale: 5, brushType: 'fill', }, label: { normal: { formatter: '{b}', position: 'right', show: false }, emphasis: { show: false } }, itemStyle: { normal: { color: '#fff' } } }, ] }; $.getJSON('../../MapCN/guangdong.json', function(chinaJson) { echarts.registerMap('guangdong', chinaJson); _chinaMap.setOption(option, true); }); }
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值