問題描述
我正在嘗試在 Android 瀏覽器上獲取地理位置,但沒有任何反應(yīng).我使用的是三星 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 }
);
}
這是我從本網(wǎng)站復(fù)制和粘貼的代碼它給了我navigator.geolocation"但是當(dāng)涉及到getCurrentPosition"時(shí),我的代碼停止工作.移動(dòng) Chrome 工作正常,但事實(shí)并非如此.我分享了我的立場(chǎng),但仍然沒有任何反應(yīng).任何幫助都會(huì)得到幫助.謝謝.
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.
感謝大家,我找到了解決方案,經(jīng)過一些 javascript 操作后,我得到了地理位置.我試圖在文檔準(zhǔn)備好之前獲取地理位置.它奏效了.
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.
推薦答案
為了獲得正確的地理位置,您必須在使用地理位置提供的值之前使該代碼塊工作,因?yàn)椴僮魇钱惒綀?zhí)行的,在這個(gè)問題我通過在其他 .js 文件之前加載我的地??理定位腳本找到了解決方案.這解決了我的問題,這個(gè)問題的另一個(gè)技巧是,當(dāng)您授予瀏覽器始終"讀取您的位置的權(quán)限時(shí),地理位置工作更加穩(wěn)定.首次加載后,您永遠(yuǎn)不會(huì)遇到地理定位錯(cuò)誤.
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.
這篇關(guān)于navigator.geolocation.getCurrentPosition 在 Android 瀏覽器上失敗的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!