问题描述
我正在尝试在 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.
推荐答案
为了获得正确的地理位置,您必须在使用地理位置提供的值之前使该代码块工作,因为操作是异步执行的,在这个问题我通过在其他 .js 文件之前加载我的地理定位脚本找到了解决方案.这解决了我的问题,这个问题的另一个技巧是,当您授予浏览器始终"读取您的位置的权限时,地理位置工作更加稳定.首次加载后,您永远不会遇到地理定位错误.
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 浏览器上失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!