Skip to content
On this page
🎨 作者:Jacinda 📔 阅读量:

天地图使用总结

地理位置名称显示

  • 🔑 关键代码

    js
    // 获取图层API
    const 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=您的密钥";
    const imageURLT = "http://t0.tianditu.gov.cn/cia_w/wmts?" +
        "SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=cia&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles" +
        "&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}" +
        "&tk=您的密钥";
    
    // 影像底图图层
    const lay = new T.TileLayer(imageURL, { minZoom: 6, maxZoom: 18 });
    // 影像注记图层
    const lay2 = new T.TileLayer(imageURLT, { minZoom: 6, maxZoom: 18 });
    const config = { layers: [lay, lay2] };
    
    // 创建地图实例
    this.map = new T.Map('yzMap', config);
    //设置中心点
    this.map.centerAndZoom(new T.LngLat(110.15114, 22.6281), 11);
    //允许鼠标双击放大地图
    this.map.enableScrollWheelZoom();
  • 📖 参考文档

地图中嵌套一个网页(如:VR,并且有点类似小程序里嵌套【web-view】)

可以引入 iframe 标签,它能够将另一个 HTML 页面嵌入到当前页面中。

html
<iframe 
    id="inlineFrameExample"
    title="Inline Frame Example"
    width="300"
    height="200"
    src="网页地址"
>
</iframe>

标记点

添加标记点

js
//循环坐标数组,创建标记点
// markerArrs 坐标数组

this.markerArrs.forEach(item => {
    //创建标注对象
    let marker = new T.Marker(new T.LngLat(parseFloat(item.longitude), parseFloat(item.latitude)));
    //往地图上添加标注
    this.map.addOverLay(marker);
})

移除标记点

js
// 获取当前地图上的所有点
let newMarker = this.map.getOverlays(); 
//移除所有标注
newMarker.forEach(item => {
    this.map.removeOverLay(item); 
})

自定义图标

javascript
// 自定义图标
let icon = new T.Icon({
    iconUrl: require('@/assets/img/icon_address2.png'), //请求图标图片的URL
    iconSize: new T.Point(49, 40), //图标可视区域的大小。
    iconAnchor: new T.Point(49, 40) //图标的定位锚点
})

// 往标记点中,添加图标
this.markerArrs.forEach((item) => {
    let marker = new T.Marker(new T.LngLat(item.longitude, item.latitude),{icon: icon}); //创建标注对象
})

点击事件

利用标记点的监听事件

js
// 如果单独写方法,需要传入两个参数,marker 坐标点, item 对应坐标点携带的内容

function( marker, item ) {
    let T = window.T;
    marker.addEventListener("click", () => {
        // panTo 视图移动到该坐标点位
        this.map.panTo(new T.LngLat(parseFloat(item.longitude), parseFloat(item.latitude)))
        let infoWin = new T.InfoWindow();
        // html内容 对应信息窗体的布局
        let sContent = ``
        // 点击坐标,打开信息弹窗
        marker.openInfoWindow(infoWin.setContent(sContent));

        // 针对sContent里的内容,写点击事件
        let length = document.getElementsByClassName('class名').length - 1
        let clickBtn = document.getElementsByClassName('class名')[length]

        clickBtn.addEventListener("click", () => {
            // 点击事件的逻辑...
        })
    })
}

对标记点携带的数组,进行画线

js
// 坐标携带的数组,此处是对后端传回来的数据做格式化,去字符串
let arrList = JSON.parse(item.addArr)
// 先定义一个空数组
let points = [];
// 对坐标携带的数组进行判断,然后遍历
if(arrList != null) {
    arrList.map(val => {
        if(val.length > 0) {
            // push进定义好的数组里
            points.push(new T.LngLat(val[0], val[1]));
        }
        
    })
}
// 创建线对象,定义线的样式
this.line = new T.Polyline(points, {
    color: '#04e246',
    weight: 3,
    opacity: 1 // 线颜色的透明度,默认不为1,所以需要自行设定
}); 
//向地图上添加线
this.map.addOverLay(this.line);

设置当前坐标数组的最佳视野

js
//poinArr marker标记点坐标数组
this.map.setViewport(poinArr);

地图点击事件,获取坐标点位

js
// 放在地图初始化函数中
var cp = new T.CoordinatePickup(this.map, {callback: this.getLngLat})
cp.addEvent();

methods: {
    getLngLat(lnglat) {
        console.log(lnglat)
    }
}

天地图报错

Error in v-on handler: “TypeError: Cannot read property ‘_tdt_events‘ of null

原因:应该是天地图写法/初始化问题

这里再次更新一下,参考以下步骤

天地图初始化封装

创建一个js文件

路径:@/utils/map.js

javascript
// 初始化地图
export default {
  init() {
		return new Promise((resolve, reject) => {
			// 如果已加载直接返回
			if (window.T) {
				console.log('地图脚本初始化成功...')
				resolve(window.T)
				reject('error')
			}
		})
	}
}

地图控件(页面)中引入

javascript
import map from '@/utils/map.js'

export default {
    created() {
        this.mapInit()
    },
    methods: {
        mapInit() {
            map.init().then( T => {
                ....
            })
        }
    }
}

引入控件函数,直接显示地图类型(与 第一个叠加两层Image效果一样)

javascript
//创建地图图层对象
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);      // 设置地图位地星混合图层
  • 🏷️ 地图选择控件 - 常量描述
常量描述
TMAP_NORMAL_MAP展示普通街道视图
TMAP_SATELLITE_MAP展示卫星视图
TMAP_HYBRID_MAP展示卫星和路网的混合视图
TMAP_TERRAIN_MAP展示地形视图
TMAP_TERRAIN_HYBRID_MAP展示地形和路网的混合视图

逆地址解析

📖 参考文档