地理位置API不适用于移动设备

问题描述 投票:0回答:2

我在React / Redux上编写我的Web应用程序。我需要借助Geolocation API获取用户位置。在桌面浏览器上一切正常,但在手机上(在小米Redmi Note 3和iPhone 5s上检查)它会抛出错误代码1 - 权限被拒绝。并且它不会请求获取该位置的任何权限。

这是我在我的网站上运行的测试样本:

componentDidMount() {
    if (window.navigator.geolocation) {
        window.navigator.geolocation.getCurrentPosition(position => {
            alert(position.coords.latitude + ' ' + position.coords.longitude);
        }, err => {
            alert('ERROR: ' + err.code);
        });
    } else {
        alert('Geolocation API is not supported!');
    }
}

这个问题的解决方案是什么?

javascript html5 reactjs geolocation
2个回答
2
投票

得到了同样的问题......解决了:

检查您的手机权限以分享您的位置。

在iPhone上:设置 - >位置服务 - > [您的浏览器]

https://support.apple.com/en-ca/HT203033

添加:

Chrome需要https才能使用地理位置:https://developers.google.com/web/updates/2016/04/geolocation-on-secure-contexts-only


0
投票

我有解决方案。我正在使用Web Application Manifest,它需要设置使用Geolocation API的权限。

我们只需要在manifest.webapp文件中设置“required_features”选项:

{
  "required_features": ["geolocation"]
}

希望它对某些人有用;)

© www.soinside.com 2019 - 2024. All rights reserved.