Rea*_*ala 5 geolocation react-native expo
在本机应用程序中允许访问 GPS 的权限后。如果用户拒绝打开 GPS。它会显示像这样的错误
未处理的承诺拒绝:错误:由于设备设置不满意,位置请求失败。”
如果用户拒绝 Gps 打开选项,我想避免它会返回一些东西。所以我需要位置的条件,无论是开还是关。(我正在使用博览会位置)
您看到此错误是因为 Location.getCurrentPositionAsyncisasync方法并且它返回一个承诺,如果失败则抛出一个错误(您看到的错误)。
您可以将代码包装在 atry和catch块中以捕获错误并对其进行处理。例子:
_getLocationAsync = async () => {
let { status } = await Permissions.askAsync(Permissions.LOCATION);
if (status !== 'granted') {
alert('The request was denied');
}else{
try{
let location = await Location.getCurrentPositionAsync({});
// do something with location
}catch(e){
alert('We could not find your position. Please make sure your location service provider is on');
console.log('Error while trying to get location: ', e);
}
}
}
// call this._getLocationAsync();
Run Code Online (Sandbox Code Playgroud)
您将需要检查 expo-location 的状态并将用户重定向到设置以允许您可以使用 android 意图 for android 和 ios 您可以使用链接将用户重定向到设备设置并授予权限
requestLocationPermission = async () => {
const { status } = await Permissions.askAsync(Permissions.LOCATION);
if (status === 'granted) {
navigation.navigate('screen_name');
} else {
// Alert Message if user does not allow permissions
Alert.alert("alert Message", "Instructions based on OS", [
{
text: 'Open Settings',
onPress: () => goToSettings(),
style: 'cancel',
},
{ text: Languages.DENY, onPress: () => navigation.goback()},
]);
}
};
Run Code Online (Sandbox Code Playgroud)
前往设置
goToSettings = () => {
if (Platform.OS == 'ios') {
// Linking for iOS
Linking.openURL('app-settings:');
} else {
// IntentLauncher for Android
IntentLauncher.startActivityAsync(
IntentLauncher.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS
);
}
};
Run Code Online (Sandbox Code Playgroud)
- 注意 Intent launcher 是一个单独的 android 包
| 归档时间: |
|
| 查看次数: |
3056 次 |
| 最近记录: |