Mapbox GL JS相机中心直接移动到用户的位置



如何让相机中心(起始位置)直接先移动到用户的位置,除了按Mapbox GL JS中的"GeolocateControl"按钮?

谢谢!

<body>
<div id='map'></div>
<script>
mapboxgl.accessToken = '...'
var map = new mapboxgl.Map({
    container: 'map', // container id
    center: [23.7548053,62.5590779], // starting position
    zoom: 2,
    style: 'mapbox://styles/mapbox/streets-v9'
});
// Add geolocate control to the map.
map.addControl(new mapboxgl.GeolocateControl({
    positionOptions: {
        enableHighAccuracy: true
    },
    trackUserLocation: true
}));
</script>    

将控件添加到映射后,您可以调用 trigger ,请参阅 https://www.mapbox.com/mapbox-gl-js/api/#geolocatecontrol#trigger 中的文档

const geolocate = new mapboxgl.GeolocateControl({
    positionOptions: {
        enableHighAccuracy: true
    },
    trackUserLocation: true
});
map.addControl(geolocate);
geolocate.trigger();

最新更新