Untitled
unknown
plain_text
a year ago
1.1 kB
3
Indexable
function requestLocation() {
// Check the permission status for geolocation
navigator.permissions.query({ name: 'geolocation' }).then((permissionStatus) => {
if (permissionStatus.state === 'granted') {
// Permission already granted
getLocation();
} else if (permissionStatus.state === 'prompt') {
// Permission not decided, can prompt for permission
navigator.geolocation.getCurrentPosition(getLocation, handleLocationError);
} else {
// Permission denied; guide user to settings
alert(
'Location access has been denied. Please enable location access in your browser settings to proceed.'
);
}
});
}
function getLocation(position) {
console.log('Latitude:', position.coords.latitude);
console.log('Longitude:', position.coords.longitude);
}
function handleLocationError(error) {
if (error.code === error.PERMISSION_DENIED) {
alert(
'Location access denied. Please enable location access in your browser settings if you want to use this feature.'
);
}
}
// Call this function to attempt to request location
requestLocation();
Editor is loading...
Leave a Comment