Asking to turn on location
Ask your Question
Geolocation.getCurrentPosition() requests permission which is good, but when that user doesn't have location turned on and permission is granted, location isn't enabled. Is there a way to ask the user to enable location?
Android Version 6.0.1 Latest react-native-geolcation version React Native 0.61.5
Ended up wrapping my Geolocation.getCurrentPosition with RNAndroidLocationEnabler from https://github.com/Richou/react-native-android-location-enabler
@puremana Can you share a sample code?
@HarshitMadhav Along the lines of this
constructor(props) {
super(props);
if (Platform.OS === 'android') {
this.getPermissions()
} else {
this.findCoordinates()
}
}
getPermissions = () => {
RNAndroidLocationEnabler.promptForEnableLocationIfNeeded({interval: 10000, fastInterval: 5000})
.then(data => {
if (data === "already-enabled") {
this.findCoordinates()
} else {
setTimeout(() => {
this.findCoordinates()
}, 1000)
}
})
};
findCoordinates = () => {
Geolocation.getCurrentPosition(
info => {
this.setState({ locationInfo: info });
},
error => {
console.log(error)
});
}
@puremana Thanks a lot..
Ended up wrapping my
Geolocation.getCurrentPositionwithRNAndroidLocationEnablerfrom https://github.com/Richou/react-native-android-location-enabler
what about ios?
Ended up wrapping my
Geolocation.getCurrentPositionwithRNAndroidLocationEnablerfrom https://github.com/Richou/react-native-android-location-enablerwhat about ios?
This was awhile ago but I believe iOS did it by default using this package. Atleast that is what my code is telling me. Does it not?
there' s another way that not using library?
Coming from 2022, is this issue solved yet?
hello @puremana I have tried with this code but I'm getting this error: TypeError: null is not an object (evaluating '_reactNativeAndroidLocationEnabler.default.promptForEnableLocationIfNeeded')
please check it why we are getting it even I had checked version issue.