Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 998
craigmoliver Avatar asked May 23 '11 02:05

craigmoliver


People also ask

How does Javascript geolocation work?

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).

What does showPosition () in Geolocation API returns?

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.

Which Javascript method is used to retrieve updates about the geographic location of a user?

The HTML Geolocation API is used to get the geographical position of a user.


2 Answers

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.

like image 174
Cristian Sanchez Avatar answered Sep 23 '22 04:09

Cristian Sanchez


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

like image 33
Endless Avatar answered Sep 21 '22 04:09

Endless