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