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)
(only works for Blink & Firefox)
http://caniuse.com/#feat=permissions-api
watchPosition
and getCurrentPosition
both accept a second callback which is invoked when there is an error. The error callback provides an argument for an error object. For permission denied, error.code
would be error.PERMISSION_DENIED
(numeric value 1
).
Read more here: https://developer.mozilla.org/en/Using_geolocation
Example:
navigator.geolocation.watchPosition(function(position) {
console.log("i'm tracking you!");
},
function(error) {
if (error.code == error.PERMISSION_DENIED)
console.log("you denied me :-(");
});
EDIT: As @Ian Devlin pointed out, it doesn't seem Firefox (4.0.1 at the time of this post) supports this behavior. It works as expected in Chrome and probably Safari etc.