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

时间:2019-01-05 21:18:02

标签: mapbox 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>    

1 个答案:

答案 0 :(得分:0)

将控件添加到地图后,您可以调用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();