javascript - React Native Geolocation GetCurrentPosition EnableHighAccuracy

标签 javascript android react-native geolocation

我正在 Android 上使用地理定位来获取用户的位置。我对 EnableHighAccuracy 设置有点困惑。基本上,为了使这项工作正常进行,我必须将 Android 模拟器的设置为“true”,将物理设备的设置为“false”。否则它就坏了,我会收到超时错误并且没有位置。

有人可以解释一下为什么会出现这种情况吗?奇怪的是,这一设置在不应该的情况下完全破坏了它。我不知道这是否与设备设置或其他有关。这对于生产来说似乎有点危险,因为这太hacky了。谢谢。

navigator.geolocation.getCurrentPosition(
 async (locationObj) => {
   //Some code
 },
 (error => Alert.alert("Could not get location"),
 { enableHighAccuracy: true, timeout: 15000 }
)

最佳答案

如果您将“enableHighAccuracy”设置为 true,那么它将使用 GPS 并且位置将准确。

这是 Geolocation 中的错误。在 Android 上会超时。如果您想要准确的位置并想要启用HighAccuracy那么您应该使用 react-native-geolocation-service

library 中所述

“创建这个库是为了尝试使用react-native当前的Geolocation API实现来解决android上的位置超时问题。”

官方也推荐site React Native

“在 Android 上,这使用 android.location API。Google 不推荐此 API,因为它比推荐的 Google Location Services API 不太准确且速度较慢。为了将其与 React Native 一起使用,请使用React-native-geolocation-service 模块。”

试试这个

...
import Geolocation from 'react-native-geolocation-service';
...

componentDidMount() {
    // Instead of navigator.geolocation, just use Geolocation.
    if (hasLocationPermission) {
        Geolocation.getCurrentPosition(
            (position) => {
                console.log(position);
            },
            (error) => {
                // See error code charts below.
                console.log(error.code, error.message);
            },
            { enableHighAccuracy: true, timeout: 15000, maximumAge: 10000 }
        );
    }
}

关于javascript - React Native Geolocation GetCurrentPosition EnableHighAccuracy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57913392/

相关文章:

javascript - 提供给 textinput 的类型 number 的无效 prop 值预期字符串 react native

Javascript - 计算对象数组中的重复项并将计数存储为新对象

javascript - 如何在另一个屏幕访问asyncStorage的Key

javascript - 为什么整数文字后跟一个点是 JavaScript 中的有效数字文字?

javascript - 在除 IE 之外的所有浏览器上都能正常工作*

javascript - 通用 JSON 对象

java - 构建文件夹中的values.xml 中发生错误

android - Kotlin 接口(interface)中的伴随对象

javascript - 事件委托(delegate)的事件冒泡

java - 从数组开始解析 JSON api - [