echarts渲染地图,根据地图编码获取区域地图
- 项目要求
- 功能实现
- 下载所需要的安装包
- 引入所需配置文件与初始化
- 引入echarts文件和所需要的数据项
- 创建echarts地图
- 完整代码
项目要求
- 默认展示主地图,点击后根据对应的编码进入省市区的地图,如果匹配不到对应的编码,则返回地图的主界面。
- 效果图


功能实现
下载所需要的安装包
复制
引入所需配置文件与初始化
省市区编码JSON数据(数据过长,有需要请移步)
引入echarts文件和所需要的数据项
| import * as echarts from "echarts"; |
| import axios from "axios"; |
| import { cityCode } from "@/components/cityCode.js"; |
复制
创建echarts地图
- 此处会用到外部链接来获取地图JSON数据(100000是主视图的地区编码):获取地图JSON数据

| |
| init() { |
| |
| |
| let path = `https: |
| const params = this.cityCode; |
| axios.get(path).then((res) => { |
| echarts.registerMap("china", res.data); |
| this.changeOptions("china"); |
| this.myChart = echarts.init(document.querySelector("#echatsMap")); |
| this.myChart.setOption(this.distributionOptions); |
| |
| this.myChart.on("click", (chinaParam) => { |
| if (!this.cityCode) { |
| this.cityCode = params; |
| this.getProvinceMapOpt("100000", ""); |
| } else { |
| const conform = this.cityCode.find((x) => |
| chinaParam.name.includes(x.name) |
| ); |
| if (conform) { |
| this.cityCode = conform.city; |
| this.getProvinceMapOpt(conform.adcode, conform.name); |
| } else { |
| this.cityCode = params; |
| this.getProvinceMapOpt("100000", ""); |
| } |
| } |
| }); |
| }); |
| window.onresize = function () { |
| if (this.myChart) this.myChart.resize(); |
| }; |
| }, |
复制
完整代码
| <template> |
| <div> |
| <div id="echatsMap" style="width: 100%; height: 800px"></div> |
| </div> |
| </template> |
| |
| <script> |
| import * as echarts from "echarts"; |
| import axios from "axios"; |
| import { cityCode } from "@/components/cityCode.js"; |
| export default { |
| name: "echatsMap", |
| components: {}, |
| data() { |
| return { |
| cityCode: cityCode, |
| myChart: "", |
| distributionOptions: "", |
| }; |
| }, |
| mounted() { |
| this.$nextTick(() => { |
| this.init(); |
| }); |
| }, |
| methods: { |
| |
| init() { |
| |
| let path = `https: |
| const params = this.cityCode; |
| axios.get(path).then((res) => { |
| echarts.registerMap("china", res.data); |
| this.changeOptions("china"); |
| this.myChart = echarts.init(document.querySelector("#echatsMap")); |
| this.myChart.setOption(this.distributionOptions); |
| |
| this.myChart.on("click", (chinaParam) => { |
| if (!this.cityCode) { |
| this.cityCode = params; |
| this.getProvinceMapOpt("100000", ""); |
| } else { |
| const conform = this.cityCode.find((x) => |
| chinaParam.name.includes(x.name) |
| ); |
| if (conform) { |
| this.cityCode = conform.city; |
| this.getProvinceMapOpt(conform.adcode, conform.name); |
| } else { |
| this.cityCode = params; |
| this.getProvinceMapOpt("100000", ""); |
| } |
| } |
| }); |
| }); |
| window.onresize = function () { |
| if (this.myChart) this.myChart.resize(); |
| }; |
| }, |
| |
| getProvinceMapOpt(provinceAlphabet, name) { |
| |
| let path = `https: |
| if (provinceAlphabet === 100000) { |
| path = "/mapJson/china.json"; |
| } |
| axios.get(path).then((res) => { |
| echarts.registerMap(name, res.data); |
| this.changeOptions(name); |
| this.myChart.setOption(this.distributionOptions, true); |
| }); |
| }, |
| |
| changeOptions(name) { |
| |
| const seriesList = [ |
| { |
| data: [{ value: [106.9, 27.7] }, { value: [105.29, 27.32] }], |
| }, |
| ]; |
| |
| const series = seriesList.map((v) => { |
| return { |
| type: "scatter", |
| coordinateSystem: "geo", |
| data: v.data, |
| |
| symbol: function (params, key) { |
| return "image://" + require("@/assets/logo.png"); |
| }, |
| symbolSize: 16, |
| }; |
| }); |
| |
| |
| this.distributionOptions = { |
| tooltip: { |
| |
| show: true, |
| trigger: "item", |
| triggerOn: "mousemove", |
| formatter: "名称:{b}<br/>坐标:{c}", |
| }, |
| series, |
| geo: { |
| map: name || "china", |
| layoutCenter: ["50%", "50%"], |
| layoutSize: "45%", |
| roam: true, |
| zoom: 2, |
| label: { |
| normal: { |
| |
| show: true, |
| textStyle: { |
| color: "#fff", |
| fontSize: 10, |
| fontFamily: "Arial", |
| }, |
| }, |
| emphasis: { |
| |
| |
| color: "#fff", |
| }, |
| }, |
| itemStyle: { |
| |
| normal: { |
| borderColor: "#fff", |
| areaColor: "#1c2f59", |
| textStyle: { |
| |
| color: "#fff", |
| }, |
| |
| |
| }, |
| emphasis: { |
| areaColor: "#1c2f59", |
| color: "#fff", |
| }, |
| }, |
| |
| }, |
| }; |
| }, |
| }, |
| }; |
| </script> |
| |
复制