vue2 使用高德地图

<template>
  <div class="map" id="container"></div>
</template>

<script>
import { getStation } from '@/api/photovoltaic'

export default {
  name: 'MapContainer',

  data() {
    return {
      map: null,
      mapMarker: {
        station: [],
        stationIsShow: true
      },

      kanbanData: {
        userInfo: []
      }
    }
  },
  watch: {
    map(val) {
      console.log('地图变更了')
      // 因为生命周期,所以存在数据
      let stations = this.kanbanData.stationList ? this.kanbanData.stationList : []
      stations.forEach(item => {
        this.addStationMarker(item.station_long, item.station_lat, item.station_name)
      })
    }
  },
  created() {
    // 看板01基础数据
    this.getKanban01Info()
  },
  mounted() {
    // 初始化地图,遮盖物地图
    this.$nextTick(_ => {
      this.mapInit()
    })
  },
  // 销毁地图
  beforeDestroy() {
    if (!this.map) {
      return
    }
    this.map.destroy()
    this.map = null
  },
  methods: {
    // 初始化地图
    mapInit() {
      let opts = {
        subdistrict: 0,
        extensions: 'all',
        level: 'city'
      }
      let district = new AMap.DistrictSearch(opts)
      district.search('徐州市', (status, result) => {
        let bounds = result.districtList[0].boundaries
        let mask = []
        for (let i = 0; i < bounds.length; i += 1) {
          mask.push([bounds[i]])
        }

        // 设置描点地图风格
        this.map = new AMap.Map('container', {
          mask: mask,
          viewMode: '3D',
          labelzIndex: 130,
          pitch: 30,
          zoom: 10,
          mapStyle: 'amap://styles/85a13bb7917149ce448e878f'
        })

        //添加描边
        for (let i = 0; i < bounds.length; i += 1) {
          new AMap.Polyline({
            path: bounds[i],
            strokeColor: '#ffffff',
            strokeWeight: 4,
            map: this.map
          })
        }
      })
    },

    //地图添加marker
    addStationMarker(station_long, station_lat, station_name) {
      let marker = new AMap.Marker({
        icon: require('../../../assets/img/kanban/icon.png'), // 自定义点标记
        position: [station_long, station_lat], // 基点位置
        offset: new AMap.Pixel(0, 0), // 设置点标记偏移量
        anchor: 'bottom-center', // 设置锚点方位
        title: station_name
      })

      this.mapMarker.station.push(marker)
      this.map.add(marker)
    },

    // 地图左下角标记
    markerListClick(markers, isShow) {
      if (isShow) {
        this.map.add(markers)
      } else {
        this.map.remove(markers)
      }
    },
    // 看板01基础数据
    getKanban01Info() {
      getStation({
        user_id: this.$store.getters.user_id
      }).then(res => {
        this.kanbanData = {
          ...this.kanbanData,
          ...res
        }
      })
    }
  }

}
</script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值