vue 腾讯地图(多个坐标,坐标选中效果)

vue 腾讯地图(多个坐标,坐标选中效果)

封装成组件 map.vue

<template>
  <div class="locationMap">
    <div id="container"
         ref="mapContainer"></div>
    <div v-show="loading"
         class="loading">
      <van-loading color="#07c160">地图加载中...</van-loading>
    </div>
  </div>
</template>

<script>
//坐标显示的样式
import Icon from '../../../public/images/Icon.png'
import Icon2 from '../../../public/images/Icon2.png'
export default {
  name: 'locationMap',
  props: { mapObj: Array },
  data() {
    return {
      Icon,
      Icon2,
      map: null,
      marker: null,
      loading: true,
    }
  },
  methods: {
    // 初始化地图
    initMap() {
      Map().then((TMap) => {
        this.loading = false
        //初始化位置
        let lat = this.mapObj[0].lat
        let lng = this.mapObj[0].lng
        let center = new TMap.LatLng(lat, lng)
        let markerList = []
        this.mapObj.map((item) => {
          let lat = item.lat
          let lng = item.lng
          let center = new TMap.LatLng(lat, lng)
       
            markerList.push({
              id: item.id, //点标记唯一标识,后续如果有删除、修改位置等操作,都需要此id
              styleId: ['Icon'], //指定样式id
              position: center, //点标记坐标位置
              properties: {
                //自定义属性
                id: item.id,
                name: item.name,
              },
              content: item.name,
            })
        })
        this.map = new TMap.Map(this.$refs.mapContainer, {
          //地图的中心地理坐标
          center,
          zoom: 16,
          disableDefaultUI: true,
        })
        //缩放控件
        let control = this.map.getControl(
          TMap.constants.DEFAULT_CONTROL_ID.ZOOM
        )
        //删除缩放控件
        // this.map.removeControl(TMap.constants.DEFAULT_CONTROL_ID.ZOOM)
        //缩放控件左上角
        control.setPosition(TMap.constants.CONTROL_POSITION.TOP_LEFT)
        //创建并初始化MultiMarker
        this.marker = new TMap.MultiMarker({
          map: this.map, //指定地图容器
          //样式定义
          styles: {
            //创建一个styleId为"myStyle"的样式(styles的子属性名即为styleId)
            Icon: new TMap.MarkerStyle({
              width: 32, // 点标记样式宽度(像素)
              height: 35, // 点标记样式高度(像素)
              anchor: { x: 16, y: 32 },
              src: Icon,
            }),
            Icon2: new TMap.MarkerStyle({
              width: 32, // 点标记样式宽度(像素)
              height: 35, // 点标记样式高度(像素)
              anchor: { x: 16, y: 32 },
              src: Icon2,
            }),
          },
          //点标记数据数组
          geometries: markerList,
        })
        //默认选中效果
          this.marker.updateGeometries([
            {
              styleId:Icon2,
              id: this.mapObj[0].id,
              position: center,
              properties:
                {
                    //自定义属性
                      id: this.mapObj[0].id,
                      name: this.mapObj[0].name,
                 },
              content: this.mapObj[0].name,
            },
          ])
        }
        this.loading = false
        // 初始化infoWindow
        if (this.showType != 'my') {
          var infoWindow = new TMap.InfoWindow({
            map: this.map,
            position: new TMap.LatLng(lat, lng),
            content: this.mapObj[0].name,
            offset: { x: 0, y: -32 }, //设置信息窗相对position偏移像素
          })
          // infoWindow.close() //初始关闭信息窗关闭
          //监听标注点击事件
          let that = this
          this.marker.on('click', function (evt) {
              //设置infoWindow
              infoWindow.open() //打开信息窗
              infoWindow.setPosition(evt.geometry.position) //设置信息窗位置
              infoWindow.setContent(evt.geometry.properties.name) //设置信息窗内容
              this.map.panTo(evt.latLng)//移动到所选的位置
             //选中数据传入父组件
              that.$emit('selectInfo', evt.geometry.properties)
              //选中效果
              that.mapObj.map((item) => {
                let lat = item.lat
                let lng = item.lng
                let center = new TMap.LatLng(lat, lng)
                  that.marker.updateGeometries([
                    {
                      styleId:
                        item.id === evt.geometry.id
                          ? 'Icon2'
                          : 'Icon',
                      id: item.id,
                      position: center,
                      properties: {
                        //自定义属性
                        id: item.id,
                        name: item.name,
                      },
                      content: item.name,
                    },
                  ])
              }) 
          })
        }
      })
    },
  },
  mounted() {
    this.initMap()
  },
}
</script>

<style scoped>
.locationMap {
  width:500px;
}
.loading {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
#container {
  width: 500px;
  min-width: 500px;
  max-width: 500px;
  height: 100vh;
  max-height: 100vh;
}
#container .smnoprint {
  display: none;
}
</style>

调用组件

  <Map :mapObj="mapObj"
        @selectInfo="selectInfo" />

实现效果

在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值