Last active
December 3, 2020 14:43
-
-
Save db0sch/bd3eecb92d23a0bb6c2c58e73f43c998 to your computer and use it in GitHub Desktop.
How to automatically geolocate user on map load (with Mapbox)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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