I have a page that contains a code that gets the current location from the device and load other stuff based on the location with this code:
if (navigator.ge
You need to check three things
Here's what we need to check:
Combining the other answers on the page, we can handle all of these scenarios.
This should get you up and running to handle all three scenarios:
/* Set up getCurrentPosition options with a timeout */
const navigatorLocationOptions = {
enableHighAccuracy: true,
timeout: 5000,
maximumAge: 0
};
/* Determine browser permissions status */
navigator.permissions.query({name:'geolocation'})
.then((result) => {
/* result.state will be 'granted', 'denied', or 'error' */
if (result.state === 'granted') {
navigator.geolocation.getCurrentPosition(pos => {
console.log('Retrieved user location', pos);
/* Got the location! Write your successful code here. */
}, (error) => {
/* System/OS location services disabled */
console.log('System/OS services disabled', navigator);
noLocationFound();
}, navigatorLocationOptions);
} else {
/* Browser location services disabled or error */
console.log('Browser location services disabled', navigator);
noLocationFound();
}
}, (error) => {
/* Browser doesn't support querying for permissions */
console.log('Browser permissions services unavailable', navigator);
noLocationFound()
});
function noLocationFound() {
/* Write code here for user location information is unavailable */
}