缩放地图时覆盖物标点偏移问题

问题描述

在地图上使用自定义标点(dom方式)后,缩放地图会造成标点位置偏移。

问题代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
const map = new AMap.Map('container')

const getMarkerContent = function (content) {
return `
<div class="custom-content-marker">
<img src="//webapi.amap.com/theme/v1.3/markers/b/mark_bs.png">
<div class="device-num">${content}</div>
</div>
`
}

const marker = new AMap.Marker({
position: new AMap.LngLat(120.148838, 30.245366),
content: getMarkerContent('西湖'),
offset: new AMap.Pixel(-13, -17),
...option
})

map.add(marker)

解决方式

增加锚点定位和偏移量。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
const map = new AMap.Map('container')

const getMarkerContent = function (content) {
return `
<div class="custom-content-marker">
<img src="//webapi.amap.com/theme/v1.3/markers/b/mark_bs.png">
<div class="device-num">${content}</div>
</div>
`
}

const marker = new AMap.Marker({
position: new AMap.LngLat(120.148838, 30.245366),
content: getMarkerContent('西湖'),
offset: new AMap.Pixel(0, -17), // 只调整第二项偏移,偏移量为自定义icon高度的一半
anchor: 'center', // 增加瞄点位置
...option
})

map.add(marker)

评论区