腾讯地图学习笔记
1. Vue中初始化地图
首先给html添加一个盒子:
<div id="tx_map" />
然后根据官网示例实现异步加载地图:
1 const mapScript = document.createElement('script')
2 mapScript.type = 'text/javascript'
3 mapScript.src = 'https://map.qq.com/api/gljs?v=1.exp&key=OB4BZ-D4W3U-B7VVO-4PJWW-6TKDJ-WPB77'
4 document.body.appendChild(mapScript)
5
6 mapScript.onload = () => {
7 console.log('mapScript加载完毕')
8 this.$nextTick(() => {
9 const map = new TMap.Map(document.getElementById('tx_map'), {
10 // 加载地图 经纬度 信息
11 center: new TMap.LatLng(latitude, longitude),
12 zoom: 15, // 设置缩放级别
13 draggable: true, // 设置是否可以拖拽
14 scrollwheel: true, // 设置是否可以滚动
15 disableDoubleClickZoom: true // 设置是否可以双击放大
16 })
17 this.map = map
18 // 地图加载完的事件
19 this.map.on('tilesloaded', () => {
20 if (!this.markerLayer) {
21 // 给地图添加点坐标
22 this.markerLayer = new TMap.MultiMarker({
23 map: this.map,
24 geometries: [{
25 id: '1',
26 'position': new TMap.LatLng(latitude, longitude) // 点标记坐标位置
27 }]
28 })
29 // 把该点位放到地图中心点
30 this.map.setCenter(new TMap.LatLng(latitude, longitude))
31 }
32 })
33 })
34 }
2. 地图搜索 jsonp
this.$jsonp(url, {
keyword: 关键字(用于在输入框中搜索一个地方),
location: 经纬度(用于在地图上点击一个点位,显示当前点位名称),
output: ‘jsonp’
}).then(res => {
res: {
data: [
location: {
lat: 纬度,
lng: 经度
}
]
}
}).catch(err => {
console.log(‘地图搜索失败’)
})
生活是痛苦的白天,死亡是凉爽的夜晚。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了