腾讯地图使用

<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
    评论
UniApp 是一种基于 Vue.js 的跨平台开发框架,它可以帮助开发者在不同的移动操作系统上快速构建应用程序。要在 UniApp 中使用腾讯地图,你可以按照以下步骤进行: 1. 在腾讯地图开放平台(https://lbs.qq.com/)上注册并创建应用,获取 AppKey。 2. 在 UniApp 项目的 manifest.json 文件中配置腾讯地图 SDK 的引入。示例代码如下: ``` "mp-weixin": { "plugins": { "tencentMapSDK": { "version": "1.3.2", "provider": "wxa8d7e593b2c3aXXX" } } }, "app-plus": { "plugins": { "tencentMapSDK": { "version": "1.3.2", "provider": "wxa8d7e593b2c3aXXX" } } } ``` 其中,"provider" 后面的值需要替换为你在腾讯地图开放平台中创建应用后获取到的 AppKey。 3. 在需要使用腾讯地图的页面中引入 SDK 并调用相关接口。示例代码如下: ``` <template> <view> <t-map id="map" :longitude="longitude" :latitude="latitude"></t-map> </view> </template> <script> export default { data() { return { longitude: 113.324520, latitude: 23.099994 } }, onLoad() { uni.getLocation({ success: (res) => { this.longitude = res.longitude; this.latitude = res.latitude; } }) } } </script> ``` 在这个示例中,在 onLoad 方法中通过 uni.getLocation 获取当前位置的经纬度,并将其赋值给 t-map 组件的 longitude 和 latitude 属性,实现在地图上展示当前位置。 以上是一个简单的示例,具体使用腾讯地图的功能,你还可以参考腾讯地图 SDK 的文档进行更多的调用和配置。希望对你有帮助!如有问题,请随时追问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值