How to get latitude and longitude using browser API in Javascript?

How to get latitude and longitude using the browser API in Javascript?





// Get GEO latitude and longitude using Javascript
const handleDetectLocation = () => {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(
(showPosition) => {
console.log(
showPosition.coords.latitude,
showPosition.coords.longitude
);
},
(error) => {
if (error.PERMISSION_DENIED) {
console.log('Permission denied!');
} else {
console.log(error);
}
}
);
} else {
console.log('Geolocation is not supported by this browser.');
}
};