我是靠谱客的博主 虚幻抽屉,这篇文章主要介绍高德地图,现在分享给大家,希望可以做个参考。

首先引入高德地图JSAPI

复制代码
1
<script src="https://webapi.amap.com/maps?v=1.3&key=你在高德官网申请的密钥"></script>
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// 初始化地图 const mapObj = new AMap.Map('map-container', { zoom: 17 // center: [lng, lat] }); // 获取当前城市信息 mapObj.getCity((data) => { console.log(data.citycode); }); // 获取定位 mapObj.plugin('AMap.Geolocation', () => { mapObj.clearMap(); const geolocation = new AMap.Geolocation({ enableHighAccuracy: true, // 是否使用高精度定位,默认:true timeout: 10000, // 超过2秒后停止定位,默认:无穷大 // maximumAge: 0, // 定位结果缓存0毫秒,默认:0 // convert: true, // 自动偏移坐标,偏移后的坐标为高德坐标,默认:true showButton: true, // 显示定位按钮,默认:true // buttonPosition: 'LB', // 定位按钮停靠位置,默认:'LB',左下角 buttonOffset: new AMap.Pixel(10, 20), // 定位按钮与设置的停靠位置的偏移量,默认:Pixel(10, 20) showMarker: true, // 定位成功后在定位到的位置显示点标记,默认:true showCircle: false, // 定位成功后用圆圈表示定位精度范围,默认:true panToLocation: true, // 定位成功后,是否把定位得到的坐标设置为地图中心点坐标默认值:true zoomToAccuracy: false // 定位成功后调整地图视野范围使定位位置及精度范围视野内可见,默认:false }); geolocation.getCurrentPosition((status, result) => { if (status === 'complete') { this.center = [result.position.lng, result.position.lat]; this.lng = result.position.lng.toString(); this.lat = result.position.lat.toString(); console.log(`getGeolocationPosition定位成功lng:${this.lng}---lat${this.lat}`); mapObj.setCenter(this.center); } else { // 获取定位失败时可能需要重新定位 // 此处可以尝试递归调用 } }); // 添加地图控件 mapObj.addControl(geolocation); // 地图点击事件监听 // AMap.event.addListener(geolocation, 'complete', (data) => { // this.center = [data.position.getLng(), data.position.getLat()]; // 当前中心点 // console.log(`addListener--complete---${this.center}`); // }); // 返回定位信息 // AMap.event.addListener(geolocation, 'error', (data) => { // console.log(`addListener--complete---${data}`); // }); // 返回定位出错信息 }); // 拖拽地图---标记目标位置 AMapUI.loadUI(['misc/PositionPicker'], (PositionPicker) => { const positionPicker = new PositionPicker({ mode: 'dragMap', map: mapObj, iconStyle: { url: 'static/assets/nail.png', // 标记图标 size: [15, 35], ancher: [7.5, 35] } }); positionPicker.on('success', (positionResult) => { // mapObj.remove(this.markers); // 拖拽时重新展示当前位置附近多车辆 console.log('delete markers'); console.log(positionResult.position); // positionPicker.start(mapObj.getBounds().getSouthWest()); }); positionPicker.on('fail', (positionResult) => { console.log(positionResult); }); positionPicker.start(); // 开始拖拽选址 mapObj.panBy(0, 1); }); // 地图显示圆圈区域 const Circle = new AMap.Circle({ center: [lng, lat], radius: 10, strokeColor: 'white', strokeWeight: 2, strokeOpacity: 0.5, fillColor: 'rgba(50,140,250,1)', fillOpacity: 0.6, zIndex: 10 }); Circle.setMap(mapObj); // 根据坐标点设置标记 const marker = new AMap.Marker({ position: [lng, lat], zIndex: 1000, offset: new AMap.Pixel(-8, -30), icon: new AMap.Icon({ size: new AMap.Size(15, 30), image: 'static/assets/nail.png', imageSize: new AMap.Size(15, 30) // offset: new AMap.Pixel(-10, -34) // icon可缺省,缺省时为默认的蓝色水滴图标, }) }); marker.setMap(mapObj);

最后

以上就是虚幻抽屉最近收集整理的关于高德地图的全部内容,更多相关高德地图内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(77)

评论列表共有 0 条评论

立即
投稿
返回
顶部