Checking if location services are enabled using react native
Solution 1
I was able to solve this by displaying an error message when the location could not be retrieved.
(error) => {
this.setState({servicesOn: false}),
console.log(error);
},
Solution 2
Use the "react native android location services dialog box" module for android
https://www.npmjs.com/package/react-native-android-location-services-dialog-box
install
npm install react-native-android-location-services-dialog-box --save
js
import LocationServicesDialogBox from "react-native-android-location-services-dialog-box";
LocationServicesDialogBox.checkLocationServicesIsEnabled({
message: "Use Location ?",
ok: "YES",
cancel: "NO"
}).then(function(success) {
console.log(success); // success => "enabled"
).catch((error) => {
console.log(error.message); // error.message => "disabled"
});
Solution 3
Do a watchPosition or getCurrentPosition, Your second argument is a callback in case of failure, and its argument is the reason it failed.
So, from there you know when and why it fails. You'll need an if for the reason if you only want to act on 'location disabled' and not 'request timed out' for example.
You can use the callback to add a flag to your state for example.
Ben Dellarocco
I am an automation engineer and co owner of a app development company named DownForce Studios
Updated on July 26, 2022Comments
-
Ben Dellarocco almost 2 years
I am currently working on an app using react native that requires location services to be on for some of its features. I have been researching if there is a simple way to test if the user has them turned on or not so I can display a different view if they aren't. I have not found any results other than ones that are specific to IOS or Android. Has anyone else found a solution to this?
-
Admin almost 6 yearswhat about IOS?
-
Johncy about 5 yearsI have used first library :). It's working fine but it will be better if we dont use any library.
-
amit pandya about 4 yearsdoes it checks the status continuously for the device? @Jun711