admin管理员组

文章数量:1550651

Vue项目引入天地图
一:
在vue的静态资源目录下的index.html中引入天地图的底图

<script type="text/javascript" src="http://api.tianditu.gov/api?v=4.0&tk=你申请的key"> 

二:

import MapInit from './MapInit.js'
export default {
      data () {
          return :{
              
          }
       },
      mounted(){

      var T = window.T;
      var imageURL = 'http://t0.tianditu.gov/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/v4.0/image/map/maptype/vector.png', //地图控件上所要显示的图层图标(默认图标大小80x80)
        'layer': window.TMAP_NORMAL_MAP //地图类型对象,即MapType。
      },
        {
          'title': '卫星',
          'icon': ' http://api.tianditu.gov/v4.0/image/map/maptype/satellite.png',
          'layer': window.TMAP_SATELLITE_MAP
        }, {
          'title': '卫星混合',
          'http': 'api.tianditu.gov/v4.0/image/map/maptype/satellitepoi.png',
          'layer': 'TMAP_HYBRID_MAP'
        }, {
          'title': '地形',
          'icon': ' http://api.tianditu.gov/v4.0/image/map/maptype/terrain.png',
          'layer': window.TMAP_TERRAIN_MAP
        },
        {
          'title': '地形混合',
          'icon': ' http://api.tianditu.gov/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/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); // 重新创建地图对象
            // 这里获取的经度纬度是实际经纬度四舍五入后的获取的
          });

            }
      }

}

三:

<div id="mapDiv"></div>
#mapDiv {
	  width: 100%;
	  height: 580px;
	}

本文标签: 天地vue