vue + echarts 实现乡镇地图

vue +echarts 实现乡镇地图
在这里插入图片描述

一、导入地图数据(我这里用的是json)

在这里插入代码片
``

<template>
  <div class="content">
      <!-- 地图 -->
        <div id="map" style="width:1000px;height: 700px; position: relative"></div>
  </div>
</template>

<script>
  import echarts from "echarts"
  import tuanfengJson from "./map.json"
  export default {
    components:{

    },
    data() {
      return {
        mapJson: [],
        collapsable: false,
        expandAll: true,
        style: {
          background: '#fff',
          color: '#5e6d82',
        },
      }
    },
    async created() {
      this.mapJson = await tuanfengJson;
      this.mapData = tuanfengJson.features.map(item => {
        return ({
          name: item.properties.name,
          value: 1000,
        })
      })
      this.init() //调用下面的 echarts初始化
    },
    mounted() {
        const  data ={
          town_name:'团风县'
        }
      this.getTowninfo(data)
    },
    methods: {
      init() {
        const  that = this;
        var myChart = echarts.init(document.getElementById('map'));
        echarts.registerMap('cityMap', this.mapJson, {}); //加载自定义边界数据地图
        var option = {
          dataRange: {
            show:false,
            x: 'left',
            y: 'bottom',
            splitList: [
              {start: 1, end: 1, color: '#87cefa'},
              {start: 2, end: 2, color: '#9dd7cd'},
              {start: 3, end: 3, color: '#b3e09f'},
              {start: 4, end: 4, color: '#c8e972'},
              {start: 5, end: 5, color: '#def244'},
              {start: 6, end: 6, color: '#f4fb17'},
              {start: 7, end: 7, color: '#ffee00'},
              {start: 8, end: 8, color: '#ffcc00'},
              {start: 9, end: 9, color: '#ffaa00'},
              {start: 10, end: 10, color: '#ff8900'},
              {start: 11, end: 11, color: '#2AB8FF'},
            ],
          },
          // tooltip: {
          //   trigger: 'item',
          //   formatter: p => {
          //     // console.log(p)
          //     let val = p.value;
          //     if (window.isNaN(val)) {
          //       val = 0;
          //     }
          //     let txtCon =
          //       "<div style='text-align:center'> 地区:" + p.name + '</div>';
          //     this.show = true
          //     return txtCon;
          //   }
          // },
          visualMap: {
            show:false,
            min: 1,
            max: 12,
            text: ['High', 'Low'],
            realtime: false,
            calculable: true,
            inRange: {
              color: ['lightskyblue', 'yellow', 'orangered']
            }
          },
          series: [{
            name: '地图',
            type: 'map',
            mapType: 'cityMap',
            zoom: 1.2,
            aspectScale: 1, //地图长度比
            data: [{
              name: '团风镇',
              value: 1
            },
              {
                name: '淋山河镇',
                value: 2
              },
              {
                name: '方高坪镇',
                value: 3
              },
              {
                name: '回龙山镇',
                value: 4
              },
              {
                name: '马曹庙镇',
                value: 5
              },
              {
                name: '上巴河镇',
                value: 6
              },
              {
                name: '总路咀镇',
                value:7
              },
              {
                name: '但店镇',
                value: 8
              },
              {
                name: '贾庙乡',
                value: 9
              },
              {
                name: '杜皮乡',
                value: 10
              },
            ],
            nameMap: {
              '团风镇': '团风镇',
              '淋山河镇': '淋山河镇',
              '方高坪镇': '方高坪镇',
              '回龙山镇': '回龙山镇',
              '马曹庙镇': '马曹庙镇',
              '上巴河镇': '上巴河镇',
              '总路咀镇': '总路咀镇',
              '但店镇': '但店镇',
              '贾庙乡': '贾庙乡',
              '杜皮乡': '杜皮乡',
            },
            itemStyle: {
              normal: {
                show: true,
                areaColor: '#031525', //地图区域的颜色 也可以颜色渐变如下的 areaColor
                borderColor: '#0CD5DD', //边界线颜色
                borderWidth: '1', //边界线的宽度
              },
              emphasis: { // 鼠标移入时区域的样式
                show:false,
                areaColor: '#2AB8FF',
                borderWidth: 0,
                color: 'green'
              }
            },
            label: {
              show: true,
              normal: {
                show: true,
                textStyle: {
                  color: '#333', //省市区字体颜色
                  fontSize:22
                }
              },
              emphasis: { //对应的鼠标悬浮效果
                show: true,
                textStyle: {
                  color: '#333' //鼠标移入对应的省市区字体颜色
                }
              }
            },
            markPoint: {
              symbol: 'image://' + require('../../assets/five-pointed-star.png'), // 自定义图片路径
              symbolSize: [38, 38], // 图片大小
              label: {
                position: 'top',
                color:'#fff'
              },
              data: [{
                name: '团风镇',
                coord: [114.873448, 30.633345],
                value: ''
              }]
            },
          }]
        }
        myChart.setOption(option);
        myChart.on("mouseover", function (params){
          if(params.data.value != undefined){
            myChart.dispatchAction({
              type: 'downplay'
            });
          }
        });
        //点击地图上区域请求(各地区信息)
        myChart.on('click', function (params) {
          console.log(params)
          var ini_len=option.series[0].data.length;
          option.series[0].data[0].value=1;
          option.series[0].data[1].value=2;
          option.series[0].data[2].value=3;
          option.series[0].data[3].value=4;
          option.series[0].data[4].value=5;
          option.series[0].data[5].value=6;
          option.series[0].data[6].value=7;
          option.series[0].data[7].value=8;
          option.series[0].data[8].value=9;
          option.series[0].data[9].value=10;
            myChart.setOption(option);
          for(var i=0;i<ini_len;i++){
            if(option.series[0].data[i].name==params.name){//如果匹配正确
              //改变颜色
              option.series[0].data[i].value=11;
              myChart.setOption(option);
            }
          }
        })
      },
    }
  }
</script>

<style lang="scss" scoped>
</style>
  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
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); }); }

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qq_38954058

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值