I created a map with a couple of markers and able to geolocate the user, that part is already done but I can't seem to find how to actually get the result of geolocating the user, all I get is the button to toggle it, but whatever goes on seems to be inside Mapbox itself. Any idea on how I can save the result of the user location to a variable so I can use it as a route starting point (using Mapbox Directions API). Best regards
1
votes
1 Answers
4
votes
var geolocate = new mapboxgl.GeolocateControl();
map.addControl(geolocate);
geolocate.on('geolocate', function(e) {
var lon = e.coords.longitude;
var lat = e.coords.latitude
var position = [lon, lat];
console.log(position);
});
https://www.mapbox.com/mapbox-gl-js/api/#geolocatecontrol.event:geolocate