12

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?

2 Answers 2

16

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/

4
  • 2
    Looks like there's a bug for this: bugzilla.mozilla.org/show_bug.cgi?id=650247
    – Paul V
    Jun 24, 2011 at 2:49
  • 1
    Well spotted Paul, but the bug has since been marked as a duplicate of 591745 (mistakenly in my opinion), and resolved. I think 650247 should be reopened because there is currently no way to know if "not now" was chosen. Sep 28, 2011 at 13:49
  • 1
    This was sending me round in circles - I can understand what Mozilla are saying, but it is a big UI no-no for me!
    – Rupert
    Nov 29, 2012 at 15:06
  • 1
    The demo does not seem to work in Firefox for some reason. The browser does not show the permission bar and no alerts are displayed. Jan 29, 2014 at 5:04
1

I don't think it's a bug, but an intentional choice when it comes to making it difficult to make websites that provides undesirable functionalities.. (as the top answer implied; IF you request again- when someone already said no- is rather annoying...)...

The difference between "not now".. and "never".. is that the programmer of the website KNOWS.. that if "not now" was triggered.. there would be an actual prompt to the user IF he sent the request again.. hence he would be able to "force" the user's hand to EITHER accept it.. or simply block data until the user agrees..

Decent and respectful programmers want to use such information to better provide a service (and to not wait for things that won't happen).. but truth is that there are enough spammers out there to overwhelm the end user..

(and there is no need to even TRY to send the request again, if it has been answered with "never".. because.. the user will not be terrorized in the same manner.. and if the site becomes sluggish and unresponsive, the user will just close it)

Ps. OH, and SERIOUS programmers might actually take a rejection as an actual.. rejection.. and store this choice somewhere.. despite the fact that "not now" is actually not intended as an ABSOLUTE rejection, but rather a "I have decided to not take any definite stand as of yet".. so.. someone who say "not now".. if the server knows of this choice and takes it as a "no".. then there might NEVER be another request sent.. despite the person WANTING to be able to reconsider at a later date)

1
  • What is a SERIOUS programmer? I am unfamiliar with the term.
    – Stephen Rauch
    Sep 4, 2018 at 0:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.