Vue2 + Echarts实现3D地图下钻

一、npm安装组件:

    "echarts": "5.4.0",
    "echarts-gl": "^2.0.9",
    "element-china-area-data": "^5.0.2",

二、Vue页面

<template>
  <div class="Map3D" id="Map3D" ref="Map3D" @click="toBack"></div>
</template>

<script>
import LongNanJson from '../../../public/static/city/longnan.json';
import axios from 'axios';
import {TextToCode} from "element-china-area-data";
import 'echarts-gl'

export default {
  data() {
    return {
      chartMap: {},
      option: null,
    };
  },
  props: {},
  created() {
  },
  mounted() {
    this.$nextTick(() => {
      this.initMap();
    });
    window.addEventListener('resize', this.resize)
  },

  beforeDestroy() {
    if (!this.chartMap) {
      return
    }
    this.chartMap.dispose()
    this.chartMap = null
    window.removeEventListener('resize', this.resize)
  },
  methods: {
    // 初始化地图
    initMap() {
      this.chartMap = this.$echarts.init(document.querySelector('.Map3D'));
      this.chartMap = this.$echarts.init(this.$refs.Map3D);
      this.$echarts.registerMap('LongNan', LongNanJson)
      this.option = {
        series: [
          {
            type: 'map3D',
            name: '地图',
            selectedMode: 'single', // 地图高亮单选
            regionHeight: 5, // 地图高度
            map: 'LongNan',
            viewControl: {
              distance: 120, // 地图视角 控制初始大小
              alpha: 50,// 倾斜角度
              rotateSensitivity: [1, 1]
            },
            label: {
              show: true, // 是否显示名字
              color: '#fff', // 文字颜色
              fontSize: 18, // 文字大小
              fontWeight: 'bold', // 文字大小
            },
            itemStyle: {
              color: '#224a7b', // 地图背景颜色
              borderWidth: 1,   // 分界线width
              borderColor: '#207fce', // 分界线颜色
              opacity: 0.72
            },
            emphasis: {
              label: {
                show: true, // 是否显示高亮
                textStyle: {
                  color: 'yellow' // 高亮文字颜色
                }
              },
              itemStyle: {
                color: '#007ee8', // 地图高亮颜色
                borderColor: '#6becf5'// 分界线颜色
              }
            },
            light: {
              main: {
                color: '#fff',
                intensity: 1,
                shadow: true,
                shadowQuality: 'high',
                alpha: 25, //
                beta: 20
              },
              ambient: {
                color: '#fff',
                intensity: 0.6
              }
            }
          }
        ]
      };
      this.chartMap.setOption(this.option)
      this.chartMap.on('click', (param) => {
        event.stopPropagation() // 阻止冒泡
        if (param.name) {
          const areaCode = TextToCode['甘肃省']['陇南市'][param.name].code;
          this.getCountyOption(areaCode)
        }
      });
    },
    // 显示各区县地图
    getCountyOption(areaCode) {
      axios.get('static/county/' + areaCode + '.json').then((result) => {
        this.$echarts.registerMap(areaCode, result.data)
        this.option.series[0].map = areaCode
        this.chartMap.setOption(this.option, true);
      });
    },
    // 点击页面, 返回显示中国地图
    toBack() {
      this.$echarts.registerMap('LongNan', LongNanJson)
      this.option.series[0].map = 'LongNan'
      this.chartMap.setOption(this.option, true);
    },

    resize() {
      this.chartMap.resize()
    },
  },
};
</script>

<style lang="scss" scoped>
.Map3D {
  width: 100%;
  height: 100%;
}
</style>

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
功能是指地图上的某个区域被点击后,能够进入该区域的详细信息页面。实现这个功能需要以下步骤: 1. 安装 echartsecharts-gl 插件 ```bash npm install echarts echarts-gl --save ``` 2. 引入 echartsecharts-gl 在需要使用 echarts 的组件或页面中,引入 echartsecharts-gl: ```javascript import echarts from 'echarts' import 'echarts-gl' ``` 3. 准备地图数据 在 echarts 官网的地图下载页面(https://echarts.apache.org/zh/download-map.html)下载需要的地图文件,并引入到项目中。 ```javascript import chinaMapJSON from './china.json' echarts.registerMap('china', chinaMapJSON) ``` 4. 配置 echarts 图表 ```javascript const chart = echarts.init(document.getElementById('map')) // 配置 echarts 图表 const option = { // 地图类型 series: [{ type: 'map3D', map: 'china', label: { show: true, textStyle: { color: 'rgba(255,255,255,1)', fontSize: 10 } }, itemStyle: { areaColor: 'rgba(0, 0, 0, 0)', borderColor: 'rgba(0, 0, 0, 0.2)' } }] } chart.setOption(option) // 点击事件 chart.on('click', function (params) { if (params.componentType === 'series') { console.log(params.name) // 输出区域名称 // 进入下一级页面 } }) ``` 5. 实现功能 在点击事件中,获取点击的区域名称,然后根据该名称查询下一级数据,进入下一级页面。 ```javascript chart.on('click', function (params) { if (params.componentType === 'series') { console.log(params.name) // 输出区域名称 // 查询下一级数据 const subData = getSubData(params.name) // 进入下一级页面 router.push({ name: 'SubPage', params: { data: subData } }) } }) ``` 以上就是在 vue2 中实现 echarts 3D 地图功能的具体步骤,需要注意的是,下功能的实现需要根据具体的业务需求进行调整和优化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值