Skip to content

Instantly share code, notes, and snippets.

@db0sch
Last active December 3, 2020 14:43
Show Gist options
  • Save db0sch/bd3eecb92d23a0bb6c2c58e73f43c998 to your computer and use it in GitHub Desktop.
Save db0sch/bd3eecb92d23a0bb6c2c58e73f43c998 to your computer and use it in GitHub Desktop.
How to automatically geolocate user on map load (with Mapbox)
const geolocateUser = (map) => {
const geolocate = new mapboxgl.GeolocateControl({
positionOptions: {
enableHighAccuracy: true
},
fitBoundsOptions: {
linear: false
},
trackUserLocation: false
});
map.addControl(geolocate);
map.on('load', function() {
geolocate.trigger();
});
geolocate.on('geolocate', function(e) {
map.flyTo({
zoom: 15,
center: [e.coords.longitude, e.coords.latitude]
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment