我正在尝试获取用户的位置,我的代码在ios中运行良好,在android中,当用户拒绝位置权限时,它不会抛出错误。
AppState.addEventListener('change', this._handleAppStateChange);
this.geoFailure, geoOptions);
this.refresh();
}
geoSuccess = (position) => { //Success callback when user allow the
location
this.setState({
ready:true,
where: {lat:
position.coords.latitude,lng:position.coords.longitude }
})
}
geoFailure = (err) => { // i am not getting any error when user
denies the location permission in the
case of android.
this.setState({error: err.message});
console.log("Errror",err)
console.log(err.message)
if(err.message='User denied access to location services'&&Platform.OS==='ios'){
this.props.screenName==='SPLASH'&&!this.state.ready?NavigatorService.navigate(LOCATION_PERMISSION):null;
}
}
refresh=()=> // Refreshing the list when the AppState becomes
active
let geoOptions = {
enableHighAccuracy: false,
timeout: 30000,
maximumAge: 60 * 60 * 24
};
this.setState({ready:false, error: null });
navigator.geolocation.getCurrentPosition( this.geoSuccess, this.geoFailure, geoOptions);
}如果ios不提供位置权限,我可以将用户导航到另一个屏幕,但在android的情况下,当用户不提供位置时,它不会给出任何错误。
我不知道该怎么做,我刚接触react原生软件。
我不明白我做错了什么。
任何帮助都将不胜感激。
发布于 2019-08-20 18:48:09
使用PermissionsAndroid
import {
PermissionsAndroid
} from 'react-native';
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
);
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
Geolocation.getCurrentPosition(
(position) => {
addLocation(position.coords);
},
(error) => {
console.error(error);
},
{ enableHighAccuracy: true, timeout: 15000, maximumAge: 10000 },
);
} else {
console.log('location permission denied');
}https://stackoverflow.com/questions/57571241
复制相似问题