天地图(二)引入地图

1、在public下的index.html中引入天地图

  <script src="http://api.tianditu.gov.cn/api?v=4.0&tk=你的密钥"></script>

2、在vue文件中写入

<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,
    };
  },
  mounted() {
    this.initMap();
  },

  methods: {
    // 初始化天地图
    initMap() {
      // vue项目需要先声明 T = window.T,不然后面无法获取到。
      let T = window.T;
      //我当前是存在多个地图所以使用模板动态地图id
      this.map = new T.Map(`mapDiv${this.currentIndex}`);
      // 设置中心点坐标
      const center = new T.LngLat(113.177, 29.354);
      this.zoom = 14; // 缩放级别
      // 传参中心点经纬度,以及放大程度,最小1,最大18
      this.map.centerAndZoom(center, this.zoom);
      this.map.setStyle("indigo"); //设置地图主体颜色indigo
      this.map.enableInertia(); //允许地图拖拽
      //创建卫星和路网的混合视图
      // this.map.setMapType(window.TMAP_HYBRID_MAP);
      //允许鼠标滚轮缩放地图
      this.map.enableScrollWheelZoom();
    },
  },
  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;
} */
</style>

效果图如下:

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值