How to detect negative user response for geolocation

后端 未结 2 1856
鱼传尺愫
鱼传尺愫 2021-01-11 13:04

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

It says that the second callback function

2条回答
  •  一个人的身影
    2021-01-11 13: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/

提交回复
热议问题