本文介绍了如何检测地理位置的负面用户响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当使用地理位置API的 navigator.geolocation.getCurrentPosition()如何处理一个否定的回应?



它说第二个回调函数在出现错误时被调用。然而,当用户选择不通过取消该功能从未被解雇的请求来透露他的位置。

看来 getCurrentPosition()无限期地等待答案。 (至少在Firefox 4中)

如何知道用户何时按下取消(或不等)

任何想法?

解决方案



您是对的,错误处理程序应该在用户拒绝位置请求时触发。传递给错误处理程序的错误对象应该包含错误代码和消息,让您知道用户拒绝了请求。但是,在位置请求对话框中选择 Not Now 选项时,我没有在FF4中看到这个。

在Chrome中,API /回调的工作方式与预期完全一致,但在Chrome中没有第三种选择。

编辑

啊好吧,我在FF4中发现了这个问题。在正常模式下(不是私人浏览),用户将会看到3个选项:


  • 总是共享

  • 不要共享

  • 现在不是


code>会正确地触发错误处理程序,但是 Not Now 不会。


这意味着如何处理它?



好吧,它看起来像用户点击不是现在,你不会得到回应。因此,我会设置一个超时,检查将由一个处理程序设置的标志。如果这个标志没有被设置(意味着处理程序没有在规定的时间内触发),那么您可以执行以下任一操作:


  1. 假设用户拒绝了请求(即使拒绝是临时的)
  2. 您可以再次询问用户的权限(通过相同的调用),用户将再次看到对话框。选项2可能是糟糕的可用性(和烦人),所以最好假设他们暂时拒绝并再次询问他们(礼貌地!)下一次他们访问该网站。






    我创建了一个JsFiddle来玩这个API:




    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?

    解决方案

    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/

    这篇关于如何检测地理位置的负面用户响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 17:10