gpt4 book ai didi

android - react native 地理位置getCurrentPosition无响应(android)

转载 作者:行者123 更新时间:2023-12-03 15:03:24 28 4
gpt4 key购买 nike

我已经阅读并测试了很多问题,但我仍然无法在 Android 上获得地理位置。

我使用 navigator.geolocation.getCurrentPosition,在 iOS 上一切正常,但在 Android 上,我对这个函数没有答案,无论是成功还是错误。

我已经安装了 react-native-permissions 以确保用户已经激活了权限,但它没有改变任何东西,因为它说一切都是“授权的”。

我注意到它来自设备的 GPS。如果我手动激活它,一切正常。但是,我找不到为用户激活它的方法。在 iOS 上,如果 GPS 未激活,我会陷入错误回调并告诉用户激活它,但在 android 上,什么都没有发生。

我不明白为什么我不能仅使用 getCurrentPosition 获得地理位置(我在 list 中有 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 }
);

}

有人有什么主意吗 ?

谢谢

最佳答案

您应该需要启用 全局定位系统 在安卓上

为了在 Android 上启用位置/gps,我可以推荐这个模块:

https://github.com/Richou/react-native-android-location-enabler

它使用标准的 Android 对话框定位:

像这样

import React, { Component } from "react";
import { Text, StyleSheet, View, Platform } from "react-native";
import RNAndroidLocationEnabler from "react-native-android-location-enabler";

export default class index extends Component {
componentDidMount() {
this.getLocation();
}

onLocationEnablePressed = () => {
if (Platform.OS === "android") {
RNAndroidLocationEnabler.promptForEnableLocationIfNeeded({
interval: 10000,
fastInterval: 5000,
})
.then((data) => {
this.getLocation();
})
.catch((err) => {
alert("Error " + err.message + ", Code : " + err.code);
});
}
};

getLocation = () => {
try {
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 }
);
} catch (error) {
this.onLocationEnablePressed();
}
};


}


关于android - react native 地理位置getCurrentPosition无响应(android),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44586560/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com