javascript - React Native Geolocation GetCurrentPosition EnableHighAccuracy
问题描述
我在 Android 上使用 Geolocation 来获取用户的位置。我对 EnableHighAccuracy 设置有点困惑。基本上,要完成这项工作,我必须将其设置为 Android 模拟器的“真”和物理设备的“假”。否则它坏了,我得到超时错误并且没有位置。
有人可以澄清为什么会这样吗?奇怪的是,这个设置在不应该的时候完全破坏了它。我不知道这是否与设备设置或其他有关。这对于生产来说似乎有点危险,因为这太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
“创建这个库是为了尝试使用 react-native 当前的 Geolocation API 实现来解决 android 上的位置超时问题。”
也推荐在React Native官网
“在 Android 上,这使用了 android.location API。Google 不推荐此 API,因为它比推荐的 Google Location Services API 更不准确且速度较慢。为了将它与 React Native 一起使用,请使用 react-native-geolocation -服务模块。”
尝试这个
...
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 }
);
}
}
推荐阅读
- powershell - PowerShell 脚本安装 PS5.0 更新然后重新运行当前脚本,提升,使用新版本的 PowerShell
- c# - 如何在 Unity2D 中检查我的 2D 角色是否在空中?
- c# - 这个测试似乎证实了访问类变量比方法变量快。为什么?
- sqlkata - 如何从 SqlKata 连接到 postgras 数据库?
- c# - 鼠标右键单击功能在 DataGridView 中不起作用
- postgresql - PostgreSQL的btree索引是如何实现多版本并发控制的?
- javascript - 为什么读取 DOM 测量值会触发布局/重排?
- html - Bootstrap“拉右”类没有按预期工作
- amazon-web-services - 如何在任务定义中为运行在 AWS ECS 上的 mosquitto-eclipse 配置健康检查?
- arrays - 如何多次列出数组中多个值的总和并找到最常见的输出?