Vue项目中引入天地图

  1. 在Vue的静态资源目录下的index.html中引入天地图的底图天地图API
  2. 在引入了Vue项目以后及将需要用到的功能从window对象下赋值给当前项目的Data;
<script type="text/javascript" src="http://api.tianditu.gov.cn/api?v=4.0&tk=你申请的key"> 
// 引入天地图底层图



export default {
      data () {
          return :{
              
          }
       },
      mounted(){

      var T = window.T;
      var imageURL = 'http://t0.tianditu.gov.cn/img_w/wmts?' +
        'SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=img&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles' +
        '&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}&tk=你申请的key';
      var lay = new T.TileLayer(imageURL, { minZoom: 1, maxZoom: 16 });
      var config = { layers: [lay] };

      this.map = new T.Map('mapDiv', config); // 地图实例
      this.map.centerAndZoom(new T.LngLat(this.lists[0].Longitude, this.lists[0].latitude), 3);
      // //允许鼠标双击放大地图
      this.map.enableScrollWheelZoom();

      //创建地图图层对象
      let mapTypeSelect = [{
        'title': '地图', //地图控件上所要显示的图层名称
        'icon': 'http://api.tianditu.gov.cn/v4.0/image/map/maptype/vector.png', //地图控件上所要显示的图层图标(默认图标大小80x80)
        'layer': window.TMAP_NORMAL_MAP //地图类型对象,即MapType。
      },
        {
          'title': '卫星',
          'icon': ' http://api.tianditu.gov.cn/v4.0/image/map/maptype/satellite.png',
          'layer': window.TMAP_SATELLITE_MAP
        }, {
          'title': '卫星混合',
          'http': 'api.tianditu.gov.cn/v4.0/image/map/maptype/satellitepoi.png',
          'layer': 'TMAP_HYBRID_MAP'
        }, {
          'title': '地形',
          'icon': ' http://api.tianditu.gov.cn/v4.0/image/map/maptype/terrain.png',
          'layer': window.TMAP_TERRAIN_MAP
        },
        {
          'title': '地形混合',
          'icon': ' http://api.tianditu.gov.cn/v4.0/image/map/maptype/terrainpoi.png',
          'layer': window.TMAP_TERRAIN_HYBRID_MAP
        }];
      var ctrl = new T.Control.MapType({ mapTypes: mapTypeSelect }); // 初始化地图类型选择控件

      this.map.addControl(ctrl); //添加地图选择控件

      this.map.setMapType(window.TMAP_HYBRID_MAP);      // 设置地图位地星混合图层

      
      this.GetMaps ()
      },
      methods: {
      GetMaps () {
        let T = window.T;

        //设置显示地图的中心点和级别
        this.map.clearOverLays();
        this.map.centerAndZoom(new T.LngLat(this.lists[0].Longitude, this.lists[0].latitude), 3);

          console.log(index);
          var icon = new T.Icon({
            iconUrl: 'http://api.tianditu.gov.cn/img/map/markerA.png',
            iconSize: new T.Point(33, 33),
            iconAnchor: new T.Point(10, 25)
          });

          var latlng = new T.LngLat(21,22,); // 设置坐标点传入经度纬度
          let marker = new T.Marker(latlng, { icon: icon });// 创建标注
          
          const that = this;
          marker.addEventListener('click', function (e) { // 监听点击事件
        
            let clientx = e.lnglat.lat; // 获取marker当前经纬度
            let clientY = e.lnglat.lng; 
            that.map.centerAndZoom(new T.LngLat(clientY, clientx), 10); // 重新创建地图对象
            // 这里获取的经度纬度是实际经纬度四舍五入后的获取的
          });

            }
      }

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值