I'm trying to create a react native app using expo.
My app needs user permission to get the current location.
I have a functional component that has useEffect() hook to get permission asynchronously.
Code below.
const [location, setLocation] = useState(null);
useEffect(() => {
(async () => {
let permissionStatus = null;
if (Platform.OS === 'ios') {
let {status} = await Permissions.askAsync(Permissions.LOCATION);
permissionStatus = status;
} else {
let {status} = await ExpoLocation.requestPermissionsAsync();
permissionStatus = status;
}
if (permissionStatus !== 'granted') {
return;
}
let location = await ExpoLocation.getCurrentPositionAsync({});
setLocation(location);
console.log(location);
})();
}, []);
It sometimes works without any flaws but some other times throws this warning:
Unhandled promise rejection: Error: Location provider is unavailable. Make sure that location services are enabled.
The app shows the permission request dialog and after agreeing it turns on the location services in the phone but still throws this warning/error.
After a thorough research on this website and some other I have added ACCESS_COARSE_LOCATION to my app.json.android.permissions config and experimented with some other values, but at the end I still faced this problem often.
It is worth to mention, with all the permutations of the configs the code works perfectly and sometimes throws this error/warning for the same config.
I'm using Expo SDK 40 and expo-location package version is ~10.0.0 and testing on Android 10 phone using Expo Go client app.
I would appreciate any insights or advices on how to solve this problem.
Feel free to ask for any clarifications.