Sometimes, we want to get GPS location from the web browser with JavaScript.
In this article, we’ll look at how to get GPS location from the web browser with JavaScript.
How to get GPS location from the web browser with JavaScript?
To get GPS location from the web browser with JavaScript, we can use the navigator.geolocation.getCurrentPosition
method.
For instance, we write
navigator.geolocation.getCurrentPosition((location) => {
console.log(location.coords.latitude);
console.log(location.coords.longitude);
console.log(location.coords.accuracy);
});
to call getCurrentLocation
with a callback that has the location
object parameter.
We get the latitude, longitude, and accuracy from the location.coords
property.
Conclusion
To get GPS location from the web browser with JavaScript, we can use the navigator.geolocation.getCurrentPosition
method.