我正在开发 react native 应用程序。如果用户从一个地方移动/导航到另一个地方,我必须获取多个用户位置。这工作正常,但是,如果用户在一段时间后禁用位置权限,例如用户进入禁用权限的设置,我必须显示一些按钮,例如启用位置,然后一旦用户点击该按钮,它应该要求请求位置权限。
但是,如果用户第一次授予权限,然后在一段时间内禁用权限,则点击按钮时,请求权限的弹出窗口不会在 Android 中显示弹出窗口。
我正在使用以下库来获取用户位置详细信息。
import Geolocation from 'react-native-geolocation-service';
// button on click method following
enableLocationHandler = () => {
if (Platform.OS === 'android') {
this.requestLocationPermissions();
} else {
Linking.openURL('app-settings:');
this.getLatitudeLongitude();
}
}
requestLocationPermissions = async () => {
if (Platform.OS === 'android') {
this.getLatitudeLongitude();
} else {
Geolocation.requestAuthorization();
this.getLatitudeLongitude();
}
}
getLatitudeLongitude() {
Geolocation.getCurrentPosition((position) => {
const initialPosition = JSON.stringify(position);
},
(error) => {
if (error.code === 1) {
this.setState({ errorMessage: 'Location permission is denied', isLoading: false });
Geolocation.clearWatch(this.watchID);
}
},
{ enableHighAccuracy: true, distanceFilter: 100, timeout: 20000, maximumAge: 1000 }
);
this.watchID = Geolocation.watchPosition((position) => {
// this.showLoader();
// console.log('position', position);
});
}
有什么建议吗?
最佳答案
在此插件react-native-geolocation-service中,android中没有声明运行时权限。这是在android中,权限对话框没有显示。
要解决此问题,请在请求获取位置之前添加此权限
import {PermissionsAndroid} from 'react-native';
async function requestAccessLocationPermission() {
try {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS. ACCESS_FINE_LOCATION,
{
title: 'Application wants location Permission',
message:
'Application wants location Permission',
buttonNeutral: 'Ask Me Later',
buttonNegative: 'Cancel',
buttonPositive: 'OK',
},
);
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
} else {
}
} catch (err) {
console.warn(err);
}
}
这对你有帮助,对我也有帮助。
关于javascript - 检查 React Native 中是否始终启用位置服务(iOS 和 Android 均如此),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58763218/