Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect negative user response for geolocation

When using geolocation API's navigator.geolocation.getCurrentPosition() how to deal with a negative response?

It says that the second callback function is called when there is an error. However when user chooses not to reveal his location by cancelling the request that function is never fired.

It seems that getCurrentPosition() waits for an answer indefinitely. (at least in Firefox 4)

How can I know when user presses cancel (or no etc.)

Any ideas?

like image 402
Sinan Avatar asked Jun 17 '11 00:06

Sinan


People also ask

How do you get geolocation without permission?

// You can go for an external service like // https://www.geolocation-db.com // They provide a geolocation service based on IP addresses // where you don't need user permission.

Which function is used to get the current position using geolocation API?

The Geolocation. getCurrentPosition() method is used to get the current position of the device.

How do I use geolocation API?

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 is navigator geolocation?

The Navigator geolocation property is used to return a geolocation object by the browser which can be used to locate a user's position. It is a read-only property and is only available on the approval of the user since it can compromise the user's privacy.


1 Answers

See edit below
You are correct, the error handler should fire when a user denies the location request. The error object passed into the error handler should contain an error code and message letting you know the user denied the request. However, I'm not seeing this in FF4 when selecting the option Not Now from the location request dialogue.

In Chrome, the API/callbacks work exactly as expected, but in Chrome there is no 3rd option.

EDIT
Ahhh okay I found a little quirk in the behavior of this in FF4. In normal mode (not private browsing), the user will be presented 3 options:

  • Always share
  • Never share
  • Not Now

Never share triggers the error handler correctly, but Not Now does not.

What does this mean and how to handle it?

Well, it looks like if the user hits Not Now, you aren't going to get a response. Therefore, I would set a timeout which checks a flag that would be set by one of the handlers. If this flag is not set (meaning the handlers didn't fire in the allotted time), you can do one of two things:

  1. Assume that the user denied the request (even though the denial was temporary)
  2. You can ask the user for permission again (via the same call) and the user will be presented with the dialog again.

Option 2 is probably bad usability (and annoying), so it is probably best to assume they denied temporarily and ask them again (politely!) the next time they visit the site.


I created a JsFiddle to play around with this API:

http://jsfiddle.net/7yYpn/11/

like image 118
Polaris878 Avatar answered Sep 28 '22 16:09

Polaris878