腾讯地图简单功能的封装

1、准备容器-css

 <div id="TMap"></div>

2、 props属性

 props: {

    disabledClicking: {

      type: Boolean,

      default: false

    },

    center: {

      type: Array,

      default() {

        return [28.661378, 121.428599]

      }

    },

    markerArr: {

      type: Array,

      default() {

        return []

      }

    }

  },

3、data数据

data() {

    return {

      map: null,

      markerLayer: null

    }

  },

4、 watch监听 

 watch: {

    markerArr: {

      handler(val) {

        if (val.length > 0) {

          this.map.setCenter(new TMap.LatLng(val[0].lat, val[0].lon))

          this.createMarker(this.formatMarkersData(val))

        } else {

          this.markerLayer.setMap(null)

        }

      },

      deep: true

    }

  },

5、 mounted 调用方法

 this.$nextTick(() => {

      this.initMap()

    })

 6、methods方法

methods: {

    // 初始化

    initMap() {

      this.map = new TMap.Map(document.getElementById('TMap'), {

        center: new TMap.LatLng(this.center[0], this.center[1]), // 设置地图中心点坐标

        zoom: 13, // 设置地图缩放级别

        viewMode: '2D'

      })

      if (this.markerArr.length) {

        this.createMarker(this.formatMarkersData(this.markerArr))

      }

      if (this.disabledClicking) return

      // 地图点击事件

      this.map.on('click', evt => {

        this.$emit('getLngLat', evt.latLng)

        this.markerArr[1] = {

          lat: evt.latLng.lat,

          lon: evt.latLng.lng

        }

        // 更新图层(打标)

        this.createMarker(

          this.formatMarkersData(this.markerArr)

        )

      })

    },

    // 回显点位

    createMarker(markers) {

      const that = this

      this.markerLayer && this.markerLayer.setMap(null)

      if (markers.length > 1) {

        this.map.setCenter(new TMap.LatLng(that.markerArr[1].lat, that.markerArr[1].lon))

      }

      this.markerLayer = new TMap.MultiMarker({

        id: 'marker-layer',

        map: this.map,

        styles: {

          markerStyle: new TMap.MarkerStyle({

            width: 35,

            height: 45,

            anchor: { x: 17, y: 32 },

            src: 'https://mapapi.qq.com/web/lbs/javascriptGL/demo/img/markerDefault.png'

          })

        },

        geometries: markers

      })

    },

    // 统一处理数据结构

    formatMarkersData(markers) {

      const arr = []

      markers.forEach((item, index) => {

        if (item.lon && item.lat) {

          arr.push({

            id: `marker${index}`,

            styleId: index === 0 ? 'myStyle' : 'markerStyle',

            position: new TMap.LatLng(item.lat, item.lon),

            properties: {

              title: `marker${index}`

            }

          })

        }

      })

      return arr

    }

  }

 7、容器样式-一定要给父容器一个高度

<style lang="scss" scoped>

  #TMap {

    width: 100%;

    height: 100%;

  }

</style>

相关推荐

  1. 地图简单功能封装

    2024-07-11 11:02:01       21 阅读
  2. uniapp使用地图标点

    2024-07-11 11:02:01       47 阅读
  3. uniapp地图路线规划

    2024-07-11 11:02:01       53 阅读
  4. react中使用地图

    2024-07-11 11:02:01       46 阅读
  5. uniapp使用地图获取地址信息

    2024-07-11 11:02:01       33 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-11 11:02:01       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-11 11:02:01       71 阅读
  3. 在Django里面运行非项目文件

    2024-07-11 11:02:01       58 阅读
  4. Python语言-面向对象

    2024-07-11 11:02:01       69 阅读

热门阅读

  1. 第四章 Redis(2023版本IDEA)

    2024-07-11 11:02:01       21 阅读
  2. Spring MVC -01

    2024-07-11 11:02:01       26 阅读
  3. 设计模式03-组合模式

    2024-07-11 11:02:01       21 阅读
  4. .net开发:NPOI生成excel文件到磁盘

    2024-07-11 11:02:01       24 阅读
  5. 应用TensorFlow简单工作流程

    2024-07-11 11:02:01       22 阅读
  6. 每个账号设置独立的cookie

    2024-07-11 11:02:01       24 阅读
  7. Jinja2模板引擎使用指南

    2024-07-11 11:02:01       28 阅读