Echarts 绘制地图省、市、区、县(以及点击显示下级,支持坐标定位)


**

Echarts 绘制地图省、市、区、县(以及点击显示下级,支持坐标定位)

**

上代码

javascript 复制代码
<template>
  <div class="mapCont">
    <div id="mapSelf" @contextmenu.prevent="disableContextMenu"></div>
  </div>
</template>
  
<script>
import * as echarts from 'echarts'
import mapData from "./addressMode/xiangyang.json";//引入json数据   下载地址http://datav.aliyun.com/tools/atlas/#&lat=37.82280243352756&lng=107.4462890625&zoom=4
import maplhk from "./addressMode/老河口市.json";
export default {
  name: "HelloWorld",
  data() {
    return {
      chart: null,
      rowData: [
        { name: "谷城县", jwd: [111.659572, 32.269471], value: [111.659572, 32.269471, 12] },
        { name: "谷城县", jwd: [111.659572, 32.200471], value: [111.659572, 32.200471, 24] },
      ]
    };
  },
  mounted() {
    this.mapInit(mapData, this.rowData)

  },
  methods: {
    mapInit(modeData, rowData) {
      this.chart = echarts.init(document.getElementById("mapSelf"));
      echarts.registerMap("xiangyang", modeData); //mapData是定义的数据初始化
      this.chart.setOption({
        // legend: {//可设置多个颜色
        //   orient: 'vertical',
        //   x: 'left',
        //   data: ['蓝点', '红点']
        // },
        // tooltip: {
        //   trigger: "item",
        //   formatter: "{b}",
        // },
        geo: {//定义地图样式
          type: "map",
          map: "xiangyang",
          aspectScale: 0.75, //长宽比
          selectedMode: "single", //表示选种模式是否为单选"mutiple"表多选
          showLegendSymbol: false, // 存在legend时显示
          label: {
            normal: {
              show: true,
              textStyle: {
                color: "#FFFFFFcc", //文字颜色
                fontSize: 16
              }
            },
            emphasis: {
              show: true,
              textStyle: {
                color: "#fff", //鼠标hover文字颜色
                fontSize: 18
              }
            }
          },
          itemStyle: {
            normal: {
              areaColor: "#0069e5", //整个背景填充颜色
              borderColor: "#ffffff" //边界颜色
            },
            emphasis: {
              areaColor: "darkorange" //鼠标hover颜色,区域选中颜色
            }
          },
        },
        series: [
          {
            name: "蓝点",
            type: "effectScatter", //影响散点
            coordinateSystem: "geo",
            symbolSize: 12,
            showEffectOn: "render",
            rippleEffect: {
              brushType: "stroke"
            },
            hoverAnimation: true,
            label: {
              normal: {
                color: "white",
                formatter: "{b}",
                position: "right",
                show: false
              },
              emphasis: {
                show: true
              }
            },
            itemStyle: {
              normal: {
                color: "white",
                shadowBlur: 10,
                shadowColor: "white" //闪烁圈的颜色
              }
            },
            data: rowData,

          },
          // {//可配置多个数据源
          //   name: "红点",
          //   type: "effectScatter", //影响散点
          //   coordinateSystem: "geo",
          //   symbolSize: 12,
          //   showEffectOn: "render",
          //   rippleEffect: {
          //     brushType: "stroke"
          //   },
          //   hoverAnimation: true,
          //   label: {
          //     normal: {
          //       color: "red",
          //       formatter: "{b}",
          //       position: "right",
          //       show: false
          //     },
          //     emphasis: {
          //       show: true
          //     }
          //   },
          //   itemStyle: {
          //     normal: {
          //       color: "red",
          //       shadowBlur: 10,
          //       shadowColor: "red" //闪烁圈的颜色
          //     }
          //   },
          //   data: [
          //     { name: "宜城市", jwd: [112.265758, 31.726944], value: [112.265758, 31.726944, 12] },
          //     { name: "枣阳市", jwd: [112.779625, 32.134729], value: [112.779625, 32.134729, 24] },
          //   ]
          // }
        ]
      });
      // 点击每个市所获取到的值,这里可进行相关操作
      this.chart.on("click", (params) => {
        console.log(params);
        if (params.name == "老河口市") {
          this.chart.dispose();
          this.mapInit(maplhk, [{ name: "老河口", jwd: [111.682704, 32.393923], value: [111.682704, 32.393923, 12] }])
        }
      });
      this.chart.on("contextmenu", (params) => {
        console.log(params);
        this.chart.dispose();
        this.mapInit(mapData, this.rowData)
      });
    },
    disableContextMenu(e) {//禁用右键显示保存图片
      e.preventDefault();
    }
  },


};
</script>
<style scoped>
.mapCont {
  width: 1000px;
  height: 800px;
}
#mapSelf {
  width: 100%;
  height: 100%;
}
</style>
相关推荐
禅思院9 分钟前
Vite vs Webpack 深度对比:从启动原理到生产构建,一篇就够了
前端·架构·前端框架
IT_陈寒10 分钟前
Vue的响应式真把我坑惨了,原来问题出在这
前端·人工智能·后端
朦胧之10 小时前
AI 编程-老项目改造篇
java·前端·后端
swipe13 小时前
从 0 到 1 实现大文件上传:分片、秒传、断点续传、暂停、重试与服务端合并
前端·javascript·面试
爱勇宝13 小时前
我做了一个只用来搜歌词的小 App
android·前端·后端
甲维斯13 小时前
用AI还原《坦克大战》并3D化升级!
前端·人工智能·游戏开发
IT_陈寒14 小时前
SpringBoot自动配置坑了我一晚上,原来问题出在这
前端·人工智能·后端
kyriewen15 小时前
AI 生成的代码能跑就行?这 5 个坑迟早炸
前端·javascript·ai编程
kisshyshy15 小时前
🍦 雪糕、食堂、火车厢:三幅漫画吃透栈、队列与链表
javascript·算法
谷子在生长15 小时前
纯血鸿蒙自定义弹窗最佳实践:从「到处复制」到「一行调用」
前端·harmonyos