If you are using Leaflet.Locate plugin, then simply refer to its documentation, which mentions that you can use Leaflet built-in location events:
You can leverage the native Leaflet events onlocationfound
and onlocationerror
to handle when geolocation is successful or produces an error. You can find out more about these events in the Leaflet documentation.
The linked tutorial provides you with a code snippet how to use the location found event to get the coordinates:
function onLocationFound(e) {
var radius = e.accuracy; // Note: accuracy is already a radius
L.marker(e.latlng).addTo(map)
.bindPopup("You are within " + radius + " meters from this point").openPopup();
L.circle(e.latlng, radius).addTo(map);
}
map.on('locationfound', onLocationFound);