【Vue-Echarts】地图调用组件 echarts4.9+ bmap + vue2.X

效果:根据经纬度显示各行政区需要表达数据的散点图
Echarts地图

踩坑:没有加载百度地图ak,导致仅显示title
在vue单页应用中,百度地图的ak认证获取需放在index.html的head里,否则地图将显示不出来(此处使用的ak密匙需申请)。
index.html

<head>
  <script type="text/javascript" src="https://api.map.baidu.com/api?v=3.0&ak=S4KKfPTez9LKGm4DtoY2BQ5527qxQ3QE"></script>
</head>

绘制echart后,可以继续调用百度API添加其他需求,如:

      let bmap = myChart.getModel().getComponent('bmap').getBMap();
      bmap.addControl(new BMap.MapTypeControl());
      bmap.addControl(new BMap.ScaleControl({anchor: BMAP_ANCHOR_TOP_LEFT}));// 左上角,添加比例尺
	  bmap.addControl(new BMap.NavigationControl());  //左上角,添加默认缩放平移控件    

在这里插入图片描述
其他类似组件可参考:百度地图API

bmap.enableContinuousZoom();                              //启用地图惯性拖拽,默认禁用
bmap.addControl(new BMap.OverviewMapControl());              //添加缩略地图控件
bmap.enableScrollWheelZoom();                            //启用滚轮放大缩小
bmap.addControl(new BMap.MapTypeControl());          //添加地图类型控件
bmap.disable3DBuilding();
bmap.centerAndZoom(point, 14);
bmap.setMapStyle({style:'normal'});  

VUE组件源文件:echartComponent.vue

<template>
  <div>
    <div style="width:60%;height:500px;" :id="echarts" class="echarts"  ref="echarts"></div>
  </div>
</template>

<script>
// 引入echarts
import echarts from 'echarts'
// 如果未在webpack的配置文件中设置bmap外部扩展,不要忘记引入bmap
import 'echarts/extension/bmap/bmap'

export default {
  name: 'EchartsComp',
  props: {},
  data () {
    return {
      value: [
        {name: "黄浦区", value:40},
        {name: "徐汇区", value:34},
        {name: "长宁区", value:234},
        {name: "静安区", value:238},
        {name: "普陀区", value:134},
        {name: "虹口区", value:44},
        {name: "杨浦区", value:255},
        {name: "闵行区", value:53},
        {name: "宝山区", value:50},
        {name: "浦东新区", value:355},
        {name: "金山区", value:34},
        {name: "松江区", value:56},
        {name: "青浦区", value:51},
        {name: "奉贤区", value:60},
        {name: "崇明区", value:24},
      ],
      geoCoordMap:{
        "黄浦区": [121.490317, 31.222771],
        "徐汇区": [121.43752, 31.179973],
        "长宁区": [121.4222, 31.218123],
        "静安区": [121.448224, 31.229003],
        "普陀区": [121.392499, 31.241701],
        "虹口区": [121.491832, 31.26097],
        "杨浦区": [121.522797, 31.270755],
        "闵行区": [121.375972, 31.111658],
        "宝山区": [121.489934, 31.398896],
        "浦东新区": [121.567706, 31.245944],
        "金山区": [121.330736, 30.724697],
        "松江区": [121.223543, 31.03047],
        "青浦区": [121.113021, 31.151209],
        "奉贤区": [121.458472, 30.912345],
        "崇明区": [121.39758, 31.62278]	     
      },
    }
  },
  methods: {
    convertData() {
      let res = [];
      let data_ = this.value;
      let geoCoordMap_ = this.geoCoordMap;
      for (let i = 0; i < data_.length; i++) {
        let geoCoord = geoCoordMap_[data_[i].name];
        if (geoCoord) {
            res.push({
                name: data_[i].name,
                value: geoCoord.concat(data_[i].value)
            });
        }
      }
      return res
    },
    drawChart () {
      const vm = this
      // 初始化echarts实例
      let myChart = echarts.init(document.getElementById(this.echarts))
      let option = {
        title: {
            text: '上海各区散点图',
            subtext: '#',
            sublink: '#',
            left: 'center'
        },
        tooltip : {
            trigger: 'item'
        },
        bmap: {
            center: [121.48, 31.22],
            zoom: 10,
            roam: true,
            mapStyle: {
                styleJson: [{
                    'featureType': 'water',
                    'elementType': 'all',
                    'stylers': {
                        'color': '#d1d1d1'
                    }
                }, {
                    'featureType': 'land',
                    'elementType': 'all',
                    'stylers': {
                        'color': '#f3f3f3'
                    }
                }, {
                    'featureType': 'railway',
                    'elementType': 'all',
                    'stylers': {
                        'visibility': 'off'
                    }
                }, {
                    'featureType': 'highway',
                    'elementType': 'all',
                    'stylers': {
                        'color': '#fdfdfd'
                    }
                }, {
                    'featureType': 'highway',
                    'elementType': 'labels',
                    'stylers': {
                        'visibility': 'off'
                    }
                }, {
                    'featureType': 'arterial',
                    'elementType': 'geometry',
                    'stylers': {
                        'color': '#fefefe'
                    }
                }, {
                    'featureType': 'arterial',
                    'elementType': 'geometry.fill',
                    'stylers': {
                        'color': '#fefefe'
                    }
                }, {
                    'featureType': 'poi',
                    'elementType': 'all',
                    'stylers': {
                        'visibility': 'off'
                    }
                }, {
                    'featureType': 'green',
                    'elementType': 'all',
                    'stylers': {
                        'visibility': 'off'
                    }
                }, {
                    'featureType': 'subway',
                    'elementType': 'all',
                    'stylers': {
                        'visibility': 'off'
                    }
                }, {
                    'featureType': 'manmade',
                    'elementType': 'all',
                    'stylers': {
                        'color': '#d1d1d1'
                    }
                }, {
                    'featureType': 'local',
                    'elementType': 'all',
                    'stylers': {
                        'color': '#d1d1d1'
                    }
                }, {
                    'featureType': 'arterial',
                    'elementType': 'labels',
                    'stylers': {
                        'visibility': 'off'
                    }
                }, {
                    'featureType': 'boundary',
                    'elementType': 'all',
                    'stylers': {
                        'color': '#fefefe'
                    }
                }, {
                    'featureType': 'building',
                    'elementType': 'all',
                    'stylers': {
                        'color': '#d1d1d1'
                    }
                }, {
                    'featureType': 'label',
                    'elementType': 'labels.text.fill',
                    'stylers': {
                        'color': '#999999'
                    }
                }]
            }
        },
        series : [
          {
            name: 'pm2.5',
            type: 'scatter',
            coordinateSystem: 'bmap',
            data: this.convertData(),
            symbolSize: function (val) {
                return val[2] / 10;
            },
            encode: {
                value: 2
            },
            label: {
                formatter: '{b}',
                position: 'right',
                show: false
            },
            itemStyle: {
                color: 'purple'
            },
            emphasis: {
                label: {
                    show: true
                }
            }
          }
        ]
      }
      // 绘制图表
      myChart.setOption(option)
    
      let bmap = myChart.getModel().getComponent('bmap').getBMap();
      bmap.addControl(new BMap.MapTypeControl());
      bmap.addControl(new BMap.ScaleControl({anchor: BMAP_ANCHOR_TOP_LEFT}));// 左上角,添加比例尺
      bmap.addControl(new BMap.NavigationControl());  //左上角,添加默认缩放平移控件
    }
  },
  computed: {
    echarts() {
      return 'echarts' + Math.random()*100000
    },
  },
  mounted() {
    this.drawChart()
  },
  created: () => {}
}
</script>
<style scoped>
</style>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值