腾讯地图使用

<template>

  <div class="tx-map" id='mapContainer'></div>

</template>

<script>

import { baseApi } from '@/api'

export default {

  name: 'TXNMap',

  props: {

    centerLatLng: { // 中心点默认西安

      type: Object,

      default() {

        return {

          lat: 34.34127,

          lng: 108.93984

        }

      }

    },

    zoom: { // 缩放比例

      type: Number,

      default: 4

    }

  },

  data() {

    return {

      map: null, // 地图实例

      polygon: null,

      cityList: [], // 城市列表

      position: {}, // 点击坐标点

      infoWindow: null, // 弹窗信息

    }

  },

  mounted() {

    this.init();

    this.getCtiyList();

  },

  methods: {

    init() {

      this.map = new window.TMap.Map('mapContainer', {

        zoom: this.zoom,

        center: new window.TMap.LatLng(this.centerLatLng.lat, this.centerLatLng.lng)

      });

      //绑定点击事件

      this.map.on("click", this.mapClick)

    },

    mapClick(evt) {

      const lat = evt.latLng.getLat().toFixed(6);

      const lng = evt.latLng.getLng().toFixed(6);

      this.position = {lat, lng};

      console.log('mapClick', evt)

      this.getCity({lat, lng});

    },

    // 获取所有城市

    getCtiyList() {

      baseApi.getList().then(res => {

        console.log(res)

        this.cityList = res

      })

    },

    // 根据坐标点获取城市

    getCity(latLng) {

      baseApi.getCityByLocation({location: `${latLng.lat},${latLng.lng}`}).then(res => {

        console.log('getCity', res)

        this.createInfoWindow(this.position, res.address_component.province, 6)

      })

    },

    // 根据城市获取path

    getGeometries(cityId) {

      return baseApi.search({get_polygon: 2, keyword: cityId})

    },

    // 重新设置中心点和zoom

    setCenter() {

      var center = new window.TMap.LatLng(this.position.lat, this.position.lng);//设置中心点坐标

      //初始化地图

      this.map.easeTo({

        zoom: 7,

        center: center

      }, {

        duration: 2000

      })

    },

    // 绘制轮廓,遮罩

    draw(mapData, cityId, color = '#2ba2c8') {

      let geometries = []

      for (let i = 0; i < mapData.length; i++) {

        var path = []

        var mapj = mapData[i]

        if (mapj && mapj.length > 0) {

          for (let j = 0; j < mapj.length / 2; j++) {

            path.push(new window.TMap.LatLng(mapj[2 * j + 1], mapj[2 * j]))

          }

        }

        geometries.push({ paths: path, id: 'polygon', styleId: 'polygon' })

      }

      

      this.polygon = new window.TMap.MultiPolygon({

        id: cityId,

        map: this.map,

        styles: {

          'polygon': new window.TMap.PolygonStyle({

            'color': color,

            'showBorder': false

          })

        },

        geometries: geometries

      });


 

      this.infoWindow.close();

      this.setCenter()

    },

    // 创建信息窗口

    createInfoWindow(latLng, cityName, number) {

      this.infoWindow && this.infoWindow.close();

      

      const id = new Date().getTime()

      const infoWindowLocation = new window.TMap.LatLng(latLng.lat, latLng.lng);//创建一个坐标

      //创建InfoWindow实例,并进行初始化

      if (!this.infoWindow) {

        this.infoWindow = new window.TMap.InfoWindow({

          map: this.map,

          position: infoWindowLocation,

          //设置infoWindow,content支持直接传入html代码,以实现各类内容格式需求

          content: `

            <div class="lla" id="content-${id}">

              <p style="background: #fff;">Hello World!${cityName}, ${number}</p>

              <input type="button" value="按钮" id="button-${id}"/>

            </div>

          `

        });

      } else {

        this.infoWindow.setPosition(infoWindowLocation);//设置信息窗位置

        const html = `

            <div class="lla" id="content-${id}">

              <p style="background: #fff;">Hello World!${cityName}, ${number}</p>

              <input type="button" value="按钮" id="button-${id}"/>

            </div>

          `

        this.infoWindow.setContent(html);//设置信息窗内容

        this.infoWindow.open()

      }

      this.$nextTick(() => {

        this.setStyle(id, latLng, cityName, number)

      })

    },

    handleClick() {

      console.log('11111')

    },

 setStyle(id, latLng, cityName, number) {

      const ele = document.querySelector(`#content-${id}`)

      ele.parentElement.style.background = 'rgba(0,0,0,.2)'

      const oButton = document.querySelector(`#button-${id}`)

      oButton.onclick = () => {

        this.$message.success(`点击的坐标${latLng.lat},${latLng.lng},城市名称:${cityName},项目数量: ${number}`)

        const cityId = this.cityList[0].find(item => item.fullname === cityName).id;

        this.getGeometries(cityId).then(res => {

          const pathData = res[0][0].polygon;

          this.draw(pathData, cityId)

        })

      }

    }

  }

}

</script>

<style lang="scss" scoped>

.tx-map {

  height: 100%;

  width: 100%;

}

</style>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值