JavaScript 地理定位在所有 Android 浏览器中失败 - 在 iOS 和 PC 中工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18107105/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 10:45:01  来源:igfitidea点击:

JavaScript Geolocation failing in all Android browsers - working in iOS and PC

javascriptandroidgeolocation

提问by TheLettuceMaster

Here is my very basic code:

这是我的非常基本的代码:

if (navigator.geolocation) {

     // WILL GET TO THIS POINT WITH TEST `ALERT()`  

      navigator.geolocation.getCurrentPosition(

     // WILL NOT GET OT THIS POINT IN ANDROID BROWSER

     function(position) {
        var latitude = position.coords.latitude;
        var longitude = position.coords.longitude;
      }, showError, {
        enableHighAccuracy: true,
        timeout : 5000,
        maximumAge: 0
       }
       );
    } else {
        return alert('No Geolocation Support.');
    }
};

It works great in iOS (Safari and Chrome); and all PC browsers I've tried.

它适用于 iOS(Safari 和 Chrome);以及我尝试过的所有 PC 浏览器。

On Android, I've tried the stock HTC Browser, Chrome and Dolphin. The Satellites look like it is searching then, it stops. I don't even recall it asking for my permission to use geolocation (could have overlooked that part)

在 Android 上,我尝试过 HTC 浏览器、Chrome 和 Dolphin。卫星看起来正在搜索,然后停止。我什至不记得它要求我允许使用地理定位(可能忽略了那部分)

UPDATE: It does work on my Nexus 10 Chrome browser. But not my HTC One X.

更新:它适用于我的 Nexus 10 Chrome 浏览器。但不是我的 HTC One X。

UPDATE 2- This appears to ONLY be happening on one Android device, an AT&T HTC One X. All other Android devices, PC browsers and iOS work fine. On the One X I get the Error Code: Timeout. Also, GPS works fine on this device otherwise.

更新 2- 这似乎只发生在一台 Android 设备上,即 AT&T HTC One X。所有其他 Android 设备、PC 浏览器和 iOS 工作正常。在 One XI 上获取错误代码:超时。此外,否则 GPS 在此设备上运行良好。

采纳答案by Exception

Your code is absolutely correct. I have tested this in Android 2.3 + to latest version in different devices including HTC, Samsung, Asus tablets and Samsung tablets. Even in tablets, It is working fine. Check if the device has settings of share location falsein device. When a page script is requesting for location browser will ask for user permission in android device browser. May be you have clicked never share your location. Please check it by clearing the browser settings in your device. I guarantee that there is no geolocation api problem in android 2.3+ without messing up with security settings.

你的代码是完全正确的。I have tested this in Android 2.3 + to latest version in different devices including HTC, Samsung, Asus tablets and Samsung tablets. 即使在平板电脑中,它也能正常工作。检查设备是否有share location falsein device 的设置。当页面脚本请求位置浏览器时,将在 android 设备浏览器中请求用户许可。可能是您点击了永不分享您的位置。请通过清除设备中的浏览器设置来检查它。我保证在 android 2.3+ 中没有地理定位 api 问题而不会弄乱安全设置。

And if still problem persists and you want add at least some polyfill then check this https://github.com/Modernizr/Modernizr/wiki/HTML5-Cross-Browser-Polyfillsand have some luck. They calculate location based on your IP usig geoIP database.

如果问题仍然存在,并且您想要至少添加一些 polyfill,请查看此https://github.com/Modernizr/Modernizr/wiki/HTML5-Cross-Browser-Polyfills并祝您好运。他们根据您的 IP usig 计算位置geoIP database

回答by Drewes

You must use HTTPS

您必须使用 HTTPS

Starting with Chrome 50, Chrome no longer supports obtaining the user's location using the HTML5 Geolocation API from pages delivered by non-secure connections. This means that the page that's making the Geolocation API call must be served from a secure context such as HTTPS.

从 Chrome 50 开始,Chrome 不再支持使用 HTML5 Geolocation API 从非安全连接提供的页面获取用户位置。这意味着进行 Geolocation API 调用的页面必须由安全上下文(例如 HTTPS)提供。

https://developers.google.com/web/updates/2016/04/geolocation-on-secure-contexts-only?hl=en

https://developers.google.com/web/updates/2016/04/geolocation-on-secure-contexts-only?hl=en

回答by Owen Pattison

After searching around for a solution to the same problem, having come to the conclusion that the timeout needs to be set much higher for android devices, i implemented the following as a base:

在四处寻找相同问题的解决方案后,得出的结论是,android 设备的超时需要设置得更高,我将以下内容作为基础实现:



    function setGeoLocation(){

    var ua = navigator.userAgent.toLowerCase(),
        isAndroid = ua.indexOf("android") > -1,
        geoTimeout = isAndroid ? '15000' : '1000';

        function success (position) {
            console.log(position);
        };

        function error (err) {
            console.log('error message');
        }

        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(success, error, {enableHighAccuracy: true, maximumAge: 3000, timeout:geoTimeout});
        } else {
            error('Location services must be enabled to use this');
        }

    };

回答by cahlander

I came across the same problem with a program that does no communication with a web site. It has a URL where it gets its code, but after loading the HTML page and the .js files, it does not communicate with the host. This program has worked for several years and just stopped working. The problem was that a new version of Chrome disabled the navigator.geolocation.getCurrentPosition() function. I changed the site to allow SSL access and the program now works as before.

我在一个不与网站通信的程序中遇到了同样的问题。它有一个用于获取代码的 URL,但在加载 HTML 页面和 .js 文件后,它不与主机通信。这个程序已经运行了几年,刚刚停止工作。问题是新版本的 Chrome 禁用了 navigator.geolocation.getCurrentPosition() 函数。我更改了站点以允许 SSL 访问,该程序现在可以像以前一样工作。

I'm really surprised that Chrome disabled this feature on the mobile device.

我真的很惊讶 Chrome 在移动设备上禁用了这个功能。

Another workaround is to use firefox as the browser.

另一种解决方法是使用 Firefox 作为浏览器。

回答by mahedhanu

I also had the same issue and I resolved it by setting enableHighAccuracy: false. Some devices do not support GEO locations when enableHighAccuracyis set to true

我也遇到了同样的问题,我通过设置enableHighAccuracy: false. enableHighAccuracy设置为时,某些设备不支持 GEO 位置true

回答by delkant

Try this: to watchPosition call, and wrapping this in a 5 second wait before clearing the watchID. Code below;

试试这个: watchPosition 调用,并在清除 watchID 之前等待 5 秒。下面的代码;

var options = { enableHighAccuracy: true, maximumAge: 100, timeout: 60000 };
if( navigator.geolocation) {
   var watchID = navigator.geolocation.watchPosition( gotPos, gotErr, options );
   var timeout = setTimeout( function() { navigator.geolocation.clearWatch( watchID ); }, 5000 );
} else {
   gotErr();
}

I haven't played around with the "options" values or the timeout delay at the moment, but the above code brings back accurate positioning info on every platform I've tried.

我目前还没有尝试过“选项”值或超时延迟,但是上面的代码在我尝试过的每个平台上都带回了准确的定位信息。

回答by Ericpoon

I also have a project about geolocation API in HTML5. I tested ZTE V880(ROM CM7 2.3),ZTE V980 ROM 4.0, Huawei P6 ROM4.2, Samgsung Galaxy Note I (ROM 2.3,ROM 4.1), found that the location service can be solve in some device. But cannot work on Galaxy Note I with Rom2.3, wifi & gps.

我还有一个关于 HTML5 中地理定位 API 的项目。我测试了中兴V880(ROM CM7 2.3)、中兴V980 ROM 4.0、华为P6 ROM4.2、三星Galaxy Note I(ROM 2.3、ROM 4.1),发现在某些设备上可以解决定位服务。但不能在带有 Rom2.3、wifi 和 gps 的 Galaxy Note I 上工作。

But some device can track the location when using Opera browser, like ZTE V980 ROM4.0

但是有些设备在使用Opera浏览器时可以跟踪位置,例如中兴V980 ROM4.0