navigator.geolocation.getCurrentPosition 在 Android 浏览器上

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

问题描述

我正在尝试在 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 浏览器上失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)