react-native - 在没有 GPS 的情况下,使用 navigator.geolocation 在 react-native 上无法获取当前位置

标签 react-native gps location expo

讨论及解答后的小结 :

使用 EXPO sdk,如果没有在 android 中授予 FINE_LOCATION,您将无法获取设备位置。 FINE_LOCATION 是获取位置的唯一方法,因此您无法获取hardware.network.location。这意味着:在 android > 6 的情况下,您无法使用 WIFI/移动网络获取当前位置,您必须启用 Location。

世博github讨论: https://github.com/expo/expo/issues/1339

最初的问题:

我正在处理 react 原生 申请, 使用 expo 和 react-native-maps ,我需要获取用户当前位置的经纬度。

我正在使用 navigator.geolocation API为了那个原因

打开 GPS 我没有问题,但我需要根据网络提供商在没有 GPS 的情况下获取当前位置。

问题是当应用程序运行时在安卓 > 6 上进行博览会我收到此错误:

21:50:53: Finished building JavaScript bundle in 449ms 21:50:55: Location services are disabled - node_modules\react-native\Libraries\BatchedBridge\NativeModules.js:80:57 in - node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:347:19 in __invokeCallback - ... 4 more stack frames from framework internals



在 IO 和 android <=5 中效果很好。

这是组件的代码:
class MyComponent extends Component {

    componentWillMount = () => {    
            this.getCurrentLocation();    
    }
    getCurrentLocation = () =>
         navigator.geolocation.getCurrentPosition(
            (position) => {
                let currentUserPosition = position.coords;
                alert(JSON.stringify(currentUserPosition));
            },
            (error) => {
                console.log(error);
            },
            {
                enableHighAccuracy: false,
                timeout: 20000,
                maximumAge: 0,
                distanceFilter: 10
            }
        );

}

这是我的 package.json 依赖项:
"dependencies": {
    "expo": "23.0.4",
    "native-base": "^2.3.5",
    "react": "16.0.0",
    "react-native": "0.50.4",
    "react-native-extend-indicator": "^0.1.2",
    "react-native-maps": "^0.19.0",
    "react-native-maps-directions": "^1.3.0",
    "react-native-router-flux": "4.0.0-beta.21",
    "react-navigation": "1.0.0-beta.21",
    "react-navigation-redux-debouncer": "^0.0.2",
    "react-redux": "^5.0.6",
    "redux": "^3.7.2",
  }

我希望 navigator.geolocation 在那种情况下(没有 gps)根据网络提供商获取位置,规范说..

我还尝试了 expo 的 Geolocation API(尝试了这个示例:https://snack.expo.io/@schazers/expo-map-and-location-example),并且在 GPS 关闭的情况下,我无法获取我的位置..

所以..有没有办法实现我想要的?我错过了什么?

编辑(世博会贡献者回答):

我在 expo github (https://github.com/expo/expo/issues/1339) 上发布了相同的内容,根据他们的说法,如果没有在 android 设备中启用任何级别的 Location ,使用 navigator.geolocation 获取当前位置是不可能的.. 所以.. 唯一可能发生的事情是不是 5 之前的 android 版本默认启用了位置,您可以只打开 GPS,而版本 6 及更高版本您必须指定位置级别..

有任何想法吗?

EDIT2(重要!!):

我已经确认了这一点:

enter image description here

这是 Android 6 设备的安全设置,默认情况下它使用 GPS,我认为 android 5 或更低版本没有,所以就是这样.. 当我使用第二个选项时,它会告诉我位置!

事实上,强制用户启用位置就像“嘿,使用你的 GPS!”,并且有很多应用程序可以在不打开位置的情况下为您提供近似位置(例如优步),所以问题是, 有没有办法只使用 wifi 来获取这个 api 的位置?

最佳答案

您在这里处理的问题是 EXPO,因为位置请求 API 在 Android 5(API 21)之后发生了变化。

在 API 21 之前,您需要添加到 ACCESS_FINE_LOCATION (Wifi/网络和 GPS 提供商)和 ACCESS_COARSE_LOCATION (仅 GPS 权限)位置权限。但是,从 Android 5 开始,api 更改为 android.hardware.location.networkandroid.hardware.location.gps .

当您的目标用户同时包含 Android 5 +/- 时。您需要将这两种权限类型添加到您的 android list 中,例如:

<manifest ... >
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <!-- for Android 5.0 (API level 21) or higher. -->
    <uses-feature android:name="android.hardware.location.gps" />
    <uses-feature android:name="android.hardware.location.network" />
</manifest>

但是,似乎世博会中的人只包括了android list 中的新权限。所以你需要做的是改变android manifest。然而,expo 只允许访问程序的纯 JS 部分,而不是 native 代码。因此,您需要从 EXPO 中分离以添加 native 模块或更改。

在博览会文档中,分离过程在 this link 中进行了解释。 ,过程很简单:
  • 使用 npm 全局安装 expo 包:
    npm install -g exp
  • 在您的项目目录中,运行分离代码,它将在您的项目目录中构建 android 和 ios 文件夹:
    exp detach

  • 然后您可以在 android list 文件中进行所需的更改。

    关于react-native - 在没有 GPS 的情况下,使用 navigator.geolocation 在 react-native 上无法获取当前位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48454305/

    相关文章:

    android-widget - 是否可以使用Ionic Framework或React Native为Android创建主屏幕小部件?

    android - 堆叠在另一个 TouchableOpacity 内的 TouchableOpacity 不可点击

    javascript - 修复 Can't perform a react state update on an unmounted component React native

    android - *被动*收听 gps 位置更新

    iphone - 兴趣点 API/Web 服务

    android - 无法使用融合位置 API 停止位置更新

    java - 将 Android 应用程序中的 GPS 功能外包给一个单独的类

    javascript - 0.59.9版本的React Native项目创建时报错: couldn't find template. config.js

    android - 我怎样才能找到从特定纬度和经度到周边地区的距离(以公里为单位)?

    IOS根据位置运行应用