How to Check If Geolocation Has Been Declined with JavaScript

Is there a way to check if geolocation has been DECLINED with Javascript?

Without prompting the user, you can use the new permission api this is available as such:

navigator.permissions.query({ name: 'geolocation' }).then(console.log)

Check if Geolocation was allowed and get Lat Lon

It isn't possible with the geolocation API but it is possible with the new permission API

This code is useful if you want to try to receive the coordinates but don't want to bother the user with a prompt dialog cuz the coordinates is not that important and may as well fallback to geoIP

function getCoords() {  return new Promise((resolve, reject) =>    navigator.permissions ?
// Permission API is implemented navigator.permissions.query({ name: 'geolocation' }).then(permission => // is geolocation granted? permission.state === "granted" ? navigator.geolocation.getCurrentPosition(pos => resolve(pos.coords)) : resolve(null) ) :
// Permission API was not implemented reject(new Error("Permission API is not supported")) )}
getCoords().then(coords => console.log(coords))

check if location setting has been turned off in users browser

Have you read http://www.w3schools.com/html/html5_geolocation.asp

What you want to do is check the errors to see if they allowed it or denied the request.

function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition,showError);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}

function showPosition(position) {
x.innerHTML = "Latitude: " + position.coords.latitude + "<br>Longitude: " + position.coords.longitude;
}

function showError(error) {
switch(error.code) {
case error.PERMISSION_DENIED:
x.innerHTML = "User denied the request for Geolocation."
break;
case error.POSITION_UNAVAILABLE:
x.innerHTML = "Location information is unavailable."
break;
case error.TIMEOUT:
x.innerHTML = "The request to get user location timed out."
break;
case error.UNKNOWN_ERROR:
x.innerHTML = "An unknown error occurred."
break;
}
}

Is there a way of detecting whether a user has already given permission to use navigator.geolocation?

What about using localStorage which should be supported by all html5 browsers (that support geoLocation)

if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(successFunction, errorFunction);
}
//Get latitude and longitude;
function successFunction(position) {
var lat = position.coords.latitude;
var long = position.coords.longitude;

localStorage['authorizedGeoLocation'] = 1;
}

function errorFunction(){
localStorage['authorizedGeoLocation'] = 0;
}

function checkauthorizedGeoLocation(){ // you can use this function to know if geoLocation was previously allowed
if(typeof localStorage['authorizedGeoLocation'] == "undefined" || localStorage['authorizedGeoLocation'] == "0" )
return false;
else
return true;
}

And then you check using the below function :

alert(checkauthorizedGeoLocation());

This is the jsfiddle if you need to check

Google Maps: If User Denies Geolocation, Then Call Another Method That Uses Public IP

You need to set your showLocationsBasedOnIP function as the error callback function for getCurrentPosition(). Like this:

<!DOCTYPE html>
<html>

<body>
<button onclick="getLocation()">Try It</button>
<script>
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition, showLocationsBasedOnIP);
} else {
showLocationsBasedOnIP();
}
}

function showPosition(position) {
console.log("showing position: ", position.coords.latitude, position.coords.longitude);
}

function showLocationsBasedOnIP() {
console.log("showing location based on IP");
}

</script>
</body>

</html>

Now your function will execute in both cases; when the user doesn't allow location permissions and when the user's browser doesn't support geolocation.

user disallowed geolocation - notify user second time

The Geolocation API is designed so that it doesn't annoy the user with repeat requests after they decline. You can reset the location warnings from the settings app, but that's about all you can do.

Once the user declines geolocation permission twice in a row, the API will assume they don't want it and not ask again.

Exact wording from the Core Location documentation:

If the user denies your application’s use of the location service, this method reports a kCLErrorDenied error. Upon receiving such an error, you should stop the location service.



Related Topics



Leave a reply



Submit