問題描述
使用geolocation API的navigator.geolocation.getCurrentPosition()
如何處理否定響應?
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.
似乎 getCurrentPosition()
無限期地等待答案.(至少在 Firefox 4 中)
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.)
有什么想法嗎?
推薦答案
見下方編輯
你是對的,當用戶拒絕位置請求時,錯誤處理程序應該觸發.傳遞給錯誤處理程序的錯誤對象應包含錯誤代碼和消息,讓您知道用戶拒絕了請求.但是,從位置請求對話框中選擇選項 Not Now
時,我在 FF4 中沒有看到這一點.
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.
在 Chrome 中,API/回調完全按預期工作,但在 Chrome 中沒有第三個選項.
In Chrome, the API/callbacks work exactly as expected, but in Chrome there is no 3rd option.
編輯
啊,好吧,我在 FF4 中的這種行為中發現了一個小怪癖.在普通模式下(非隱私瀏覽),用戶將看到 3 個選項:
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:
- 永遠分享
- 從不分享
- 現在不行
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?
好吧,看起來如果用戶點擊Not Now
,您將不會得到響應.因此,我將設置一個超時來檢查將由其中一個處理程序設置的標志.如果未設置此標志(意味著處理程序未在分配的時間內觸發),您可以執行以下兩種操作之一:
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:
- 假設用戶拒絕了請求(即使拒絕是暫時的)
- 您可以再次請求用戶許可(通過相同的調用),然后用戶將再次看到該對話框.
選項 2 的可用性可能很差(而且很煩人),因此最好假設他們暫時拒絕并在下次訪問該網站時再次詢問(禮貌!).
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.
我創建了一個 JsFiddle 來玩這個 API:
I created a JsFiddle to play around with this API:
http://jsfiddle.net/7yYpn/11/
這篇關于如何檢測用戶對地理位置的負面反應的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!