echarts实现单个城市下所有区域地图

本文通过Vue.js和ECharts库展示了如何创建一个地图组件,动态高亮显示南京市下属各区。数据来源于DataV.GeoAtlas,地图区域数据以南京市为例,包括各区的学校数量、学生人数等信息。遇到的难点包括阿里云的防盗链问题和地图数据的匹配。解决方案包括本地存储地图数据文件、确保数据源与地图数据的一致性等。
摘要由CSDN通过智能技术生成

 

一:需求:展示中国某一个市下面所有的区域地图并轮播高亮显示(以南京市为例)

二:数据来源  DataV.GeoAtlas地理小工具系列(写这篇文章的时候提示服务升级)

三:具体实现如下(vue实现)

dom部分:<div style="width: 300px; height: 300px" ref="chartsDOM"></div>

data 数据部分:

data() {

    return { 

      MAP_URL: "https://geo.datav.aliyun.com"

      cityCode: "320100",//城市的code编码在第二部分数据来源中获得

      mapChart: null,//地图实例

      mapChartInterval: null,//定时器

      mapData: [//地图区域部分的数据

        {

          name: "六合区",

          schoonNum: 20,

          studentNum: 501,

          averageScore: 50.5,

          rate: "77.8%",

        },

        {

          name: "鼓楼区",

          schoonNum: 19,

          studentNum: 5001,

          averageScore: 30,

          rate: "28.8%",

        },

        {

          name: "浦口区",

          schoonNum: 18,

          studentNum: 2574,

          averageScore: 58.5,

          rate: "29.8%",

        },

        {

          name: "栖霞区",

          schoonNum: 17,

          studentNum: 158,

          averageScore: 78.5,

          rate: "78.8%",

        },

        {

          name: "玄武区",

          schoonNum: 23,

          studentNum: 6874,

          averageScore: 69.5,

          rate: "39.8%",

        },

        {

          name: "建邺区",

          schoonNum: 37,

          studentNum: 2589,

          averageScore: 88.5,

          rate: "58.8%",

        },

        {

          name: "秦淮区",

          schoonNum: 32,

          studentNum: 258,

          averageScore: 18.5,

          rate: "99.8%",

        },

        {

          name: "雨花台区",

          schoonNum: 54,

          studentNum: 259,

          averageScore: 89.5,

          rate: "77.8%",

        },

        {

          name: "江宁区",

          schoonNum: 38,

          studentNum: 2589,

          averageScore: 36.5,

          rate: "94.8%",

        },

        {

          name: "溧水区",

          schoonNum: 29,

          studentNum: 7895,

          averageScore: 78.5,

          rate: "19.8%",

        },

        {

          name: "高淳区",

          schoonNum: 33,

          studentNum: 2988,

          averageScore: 28.5,

          rate: "28.8%",

        },

      ]

    };

  }

js部分:

methods:{

   initMap() {

      let that = this;

      // 初始化统计图对象

      that.mapChart = this.$echarts.init(this.$refs["chartsDOM"]);

      // 显示 loading 动画

      that.mapChart.showLoading();

      // 再得到数据的基础上,进行地图绘制

      axios

        // .get(this.MAP_URL + "/areas_v3/bound/" + this.cityCode + "_full.json") //本地调试可以使用,发布到服务器后由于阿里云的防盗链接口会403

        .get("static/areaMap/" + this.cityCode + "_full.json") //将所需的城市地图文件下载下来放本地

        .then((res) => {

          // 得到结果后,关闭动画

          that.mapChart.hideLoading();

          // 注册地图(数据放在axios返回对象的data中哦)

          this.$echarts.registerMap(" NANJIN", res.data);

          let option = {

            tooltip: {

              trigger: "item",

              borderColor: "#1FC9F3",

              formatter: function (params) {

                let result = params.data;

                return (res =

                  "<div>" +

                  result.name +

                  "</div>" +

                  '<div style="text-align:left;">' +

                  "学校个数:" +

                  result.schoonNum +

                  "<br>" +

                  "学校学生:" +

                  result.studentNum +

                  "<br>" +

                  "平均得分:" +

                  result.averageScore +

                  "<br>" +

                  "得分率:" +

                  result.rate +

                  "</div>");

              },

            },

            series: [

              {

                type: "map",

                map: "NANJIN", // 这个是上面注册时的名字哦,registerMap('这个名字保持一致')

                // aspectScale: 1,//地图长度比默认0.75

                zoom: 1,

                roam: true,

                scaleLimit: {//地图缩放

                  min: 1,

                  max: 3,

                },

                label: {

                  show: true,

                  normal: {

                    //正常时的样式

                    show: false,

                    textStyle: {

                      color: "#333",

                    },

                  },

                  emphasis: {

                    //高亮时的样式设置

                    show: true,

                    textStyle: {

                      color: "#fff",

                      fontSize: 14,

                    },

                  },

                },

                itemStyle: {

                  normal: {

                    areaColor: "#fff",

                    borderColor: "#03b8c0",

                  },

                },

                emphasis: {

                  //高亮状态下的样式

                  itemStyle: {

                    //多边形样式

                    areaColor: "#1FC9F3",

                  },

                },

                data: this.mapData,

              },

            ],

          };

          that.mapChart.setOption(option);

          if (this.mapChartInterval) {

            window.clearInterval(that.mapChartInterval);

          }

          let currentIndex = -1;

          this.mapChartInterval = setInterval(function () {

            let dataLen = option.series[0].data.length;

            // 取消之前高亮的图形

            that.mapChart.dispatchAction({

              type: "downplay",

              seriesIndex: 0,

              dataIndex: currentIndex,

            });

            currentIndex = (currentIndex + 1) % dataLen;

            // 高亮当前图形

            that.mapChart.dispatchAction({

              type: "highlight",

              seriesIndex: 0,

              dataIndex: currentIndex,

            });

            // 显示 tooltip

            that.mapChart.dispatchAction({

              type: "showTip",

              seriesIndex: 0,

              dataIndex: currentIndex,

            });

          }, 2000);

        });

    },

}

四:难点,易出错说明

1. 采用直接访问数据来源的方式上线后由于阿里云的防盗链机制会出现接口403

        解决办法:1.阿里云官网花钱购买服务2.后端处理转发接口3.下载城市json文件到本地项目

2.mapData数据源中name(区县的名称)一定要和json文件里的相同

3.注册时 this.$echarts.registerMap(" NANJIN", res.data);其中的名字(NANJIN)要和series中 map: "NANJIN",相同

4.数据来源 可以通过此链接直接浏览器访问获取https://geo.datav.aliyun.com/areas_v3/bound/340100_full.jsonicon-default.png?t=M85Bhttps://geo.datav.aliyun.com/areas_v3/bound/340100_full.json(不同城市替换code就行)

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); }); }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值