javascript - 即使在 map 移动期间也保持标记居中

标签 javascript mapbox-gl-js

我需要始终保持标记居中,即使在拖动和平移 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/

相关文章:

javascript - 为什么不能 println();用作变量?

javascript - event.preventDefault() 不起作用。

javascript - 循环遍历主干中的阵列模型

javascript - 使标记弹出

vue.js - 如何将自定义图像添加到 map 框标记?

javascript - 将 'null' 返回到我的服务器

javascript - 在 UIManager 中找不到 RNCSafeAreaView

mapbox-gl-js - 如何设置 mapbox-gl-draw 控件的位置?

leaflet - 找不到我的 Mapbox 样式 map 的 Mapbox Vector Tile URL