天地图(四)拖拽点位获取当前经纬度以及地址名称

<template>
  <div
    :id="'mapDiv' + currentIndex"
    class="map"
    style="position: absolute; width: 100%; height: 100%; z-index: 101"
  ></div>
</template>

<script>
export default {
  props: ["currentIndex"],
  data() {
    return {
      map: null,
      center: {},
      centerIcon: {}, //中心点图标
      centerMarker: {}, //中心点标记
      mapIncon4: require("../../../../public/img/address_icon.png"),
    };
  },
  mounted() {
    this.initMap();
  },

  methods: {
    // 初始化天地图
    initMap() {
      // vue项目需要先声明 T = window.T,不然后面无法获取到。
      let T = window.T;
      this.map = new T.Map(`mapDiv${this.currentIndex}`);
      // 设置中心点坐标
      this.center = new T.LngLat(113.177, 29.354);
      this.getGeocoderFun(113.177, 29.354);

      this.zoom = 14; // 缩放级别
      // 传参中心点经纬度,以及放大程度,最小1,最大18
      this.map.centerAndZoom(this.center, this.zoom);
      this.map.setStyle("indigo"); //设置地图主体颜色indigo
      this.map.enableInertia(); //允许地图拖拽
      //创建卫星和路网的混合视图
      // this.map.setMapType(window.TMAP_HYBRID_MAP);
      //允许鼠标滚轮缩放地图
      this.map.enableScrollWheelZoom();

      this.drawPoint();
    },
    // 根据经纬度获取地址
    async getGeocoderFun(lon, lat) {
      let T = window.T;
      let geocoder = new T.Geocoder();
      geocoder.getLocation(new T.LngLat(lon, lat), (result) => {
        console.log(result.getAddress());
      });
    },
    drawPoint() {
      let T = window.T;
      this.center = new T.LngLat(113.177, 29.354);
      //创建图片对象
      this.centerIcon = new T.Icon({
        iconUrl: this.mapIncon4,
        iconSize: new T.Point(40, 40),
        iconAnchor: new T.Point(25, 25),
      });
      //向地图上添加自定义标注
      this.centerMarker = new T.Marker(this.center, {
        icon: this.centerIcon,
      });
      this.map.addOverLay(this.centerMarker);
      this.centerMarker.enableDragging();
      this.centerMarker.addEventListener("dragstart", this.dragStart);
      this.centerMarker.addEventListener("dragend", this.dragEnd);
    },
    dragStart() {
      console.log("start");
    },
    dragEnd(e) {
      console.log("end", e);
      console.log(e.lnglat.getLng(), e.lnglat.getLat());
      this.getGeocoderFun(e.lnglat.getLng(), e.lnglat.getLat());
    },
  },
  beforeDestroy() {
    this.map = null;
  },
  destroyed() {
    this.map = null;
  },
};
</script>

<style lang="scss" scoped>
::v-deep .tdt-control-container {
  display: none !important;
}

/* ::v-deep .tdt-container{
  background-color: rgba(15, 30, 80, .9) !important;
} */

.map {
  background-color: rgba(15, 44, 80, 0.9) !important;
}
</style>

效果图:

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值