navigator.geolocation.getCurrentPosition在Android浏览器上失败 [英] navigator.geolocation.getCurrentPosition fails on Android Browser

查看:91
本文介绍了navigator.geolocation.getCurrentPosition在Android浏览器上失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Android浏览器上获取地理位置信息,但没有任何反应。我使用的是Samsung Galaxy S3,但不确定浏览器的版本。 Android版本是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浏览器上失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆