I need JavaScript to display a manual entry if geolocation is declined.
What I have tried:
Modernizr.geolocation navigator.geolocation
Neither describes if user has previously declined access to geolocation.
The Geolocation API is accessed via a call to navigator. geolocation ; this will cause the user's browser to ask them for permission to access their location data. If they accept, then the browser will use the best available functionality on the device to access this information (for example, GPS).
Explanation: showPosition() method returns both latitude and longitude of user. Syntax is navigator. geolocation. getCurrentPosition(showPosition); The value of latitude and longitude returned will be in decimal.
The HTML Geolocation API is used to get the geographical position of a user.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With