【发布时间】:2017-11-19 01:44:24
【问题描述】:
我已经阅读并测试了很多问题,但我仍然无法在 Android 上获取地理位置。
我使用 navigator.geolocation.getCurrentPosition,在 iOS 上一切正常,但在 Android 上我对这个函数没有任何答案,无论是成功还是错误。
我已经安装了 react-native-permissions 以确保用户已经激活了权限,但它并没有改变任何东西,因为它说一切都是“授权的”。
我注意到它来自设备的 GPS。如果我手动激活它,一切正常。但是,我找不到为用户激活它的方法。在 iOS 上,如果 GPS 未激活,我会陷入错误回调并告诉用户激活它,但在 android 上,什么都没有发生。
我不明白为什么我不能仅使用 getCurrentPosition 获取地理位置(我在清单中有 ACCESS_COARSE_LOCATION 和 ACCESS_FINE_LOCATION)。
这是我的代码的一部分:
componentDidMount() {
navigator.geolocation.getCurrentPosition(
(position) => {
//do my stuff with position value
},
(error) => {
Permissions.getPermissionStatus('location')
.then((response) => {
if (response !== "authorized") {
Alert.alert(
"Error",
"We need to access to your location",
[
{
text: "Cancel",
onPress: () => {
// do my stuff
}, style: 'cancel'
},
{
text: "Open Settings",
onPress: () => Permissions.openSettings()
}
]
);
} else {
// do my stuff
}
});
},
{ enableHighAccuracy: true, timeout: 2000, maximumAge: 1000 }
);
}
有人知道吗?
谢谢
【问题讨论】:
-
模拟器还是设备?设置坐标时,鸸鹋的地理位置似乎不起作用。我无法让他们通过我的 RN 应用程序。
-
你找到解决办法了吗?
-
@Marina.A 你在 batiment 里面吗?如果是这种情况,GPS 定位可能需要超过 1 分钟(在 android 上)。
标签: android react-native geolocation