我需要始终保持标记居中,即使在拖动和平移 map 期间也是如此,并根据 map 移动更新其 LngLat。
虽然大部分都有效,但我就是不知道如何在 map 拖动/平移期间保持标记居中。
这是我到目前为止所得到的: https://jsfiddle.net/iaezzy/yLf4wgzd/1/
var map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/mapbox/streets-v9',
center: [0, 0],
zoom: 2
});
var marker = new mapboxgl.Marker()
.setLngLat([0, 0])
.addTo(map);
map.on('movestart', function (e) {
console.log(`Current Map Center: ${map.getCenter()}`);
marker.setLngLat(map.getCenter());
});
map.on('moveend', function (e) {
console.log(`Current Map Center: ${map.getCenter()}`);
marker.setLngLat(map.getCenter());
});
我能想到的一种方法是每隔几毫秒轮询一次map.isMoving
并更新标记位置,但这似乎不是最佳解决方案。
最佳答案
您可以使用move
map 移动时更新标记位置的 map 事件。这是your example已更新。
关于javascript - 即使在 map 移动期间也保持标记居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54383157/