問題描述
我正在嘗試在 Android 瀏覽器上獲取地理位置,但沒有任何反應.我使用的是三星 Galaxy S3,但我不確定我的瀏覽器版本.安卓版本為 4.1.2這是我的代碼:
i'm trying to get the geolocation on Android Browser but nothing happens. I'm using a Samsung Galaxy S3 but i'm not sure about the version of my browser. Android version is 4.1.2 Here is my Code:
if (navigator.geolocation) {
var timeoutVal = 10 * 1000 * 1000;
navigator.geolocation.getCurrentPosition(
displayPosition,
displayError,
{ enableHighAccuracy: true, timeout: timeoutVal, maximumAge: 0 }
);
}
這是我從本網站復制和粘貼的代碼它給了我navigator.geolocation"但是當涉及到getCurrentPosition"時,我的代碼停止工作.移動 Chrome 工作正常,但事實并非如此.我分享了我的立場,但仍然沒有任何反應.任何幫助都會得到幫助.謝謝.
this is a code i copied and pasted from this site it gives me the "navigator.geolocation" but when it comes to "getCurrentPosition" my code stops working. Mobile Chrome works fine but this is not. I shared my position but still nothing happens. Any help will be appriciated. Thanks.
感謝大家,我找到了解決方案,經過一些 javascript 操作后,我得到了地理位置.我試圖在文檔準備好之前獲取地理位置.它奏效了.
Thanks everyone i found the solution, i was getting the geolocation after some javascript operations. I tried to get the geolocation before document is ready. And it worked.
推薦答案
為了獲得正確的地理位置,您必須在使用地理位置提供的值之前使該代碼塊工作,因為操作是異步執(zhí)行的,在這個問題我通過在其他 .js 文件之前加載我的地??理定位腳本找到了解決方案.這解決了我的問題,這個問題的另一個技巧是,當您授予瀏覽器始終"讀取您的位置的權限時,地理位置工作更加穩(wěn)定.首次加載后,您永遠不會遇到地理定位錯誤.
In order to get the geolocation without errors, you have to make that code block work before using the values provided by the geolocation because operations are carried out asynchronously, in this question i found the solution by loading my geolocation script before other .js files. This solved my problem and another trick for this issue is, geolocation works more stable when you give "always" permission for browser to read your location. After loading for the first time, you never encounter geolocation errors.
這篇關于navigator.geolocation.getCurrentPosition 在 Android 瀏覽器上失敗的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!