0

I'm using navigator.geolocation.getCurrentPosition() in react native and it's working fine.
But i would like to be able to react nicely if location services are turned off (a dialog box that leads the user to settings).
I found that plugin for android and i think it's going to work fine : https://www.npmjs.com/package/react-native-android-location-services-dialog-box
How should i do in iOS ?

alexandre
  • 195
  • 4
  • 15

1 Answers1

-1

EDIT

You can use this very well done 3rd party library

and then use it like this :

Location.getAuthorizationStatus((authorization) => {
  console.log(authorization)
});

Previous answer

getCurrentPosition can take an error callback. This will be triggered if the location is not enabled.

You can do something like:

navigator.geolocation.getCurrentPosition(
  (position) => {
    this.setState({ position });
  },
  (error) => {
    console.log(alert);
  },
  {enableHighAccuracy: true, timeout: 20000, maximumAge: 1000}
);

You can check the docs for more informations.

G. Hamaide
  • 7,078
  • 2
  • 36
  • 57
  • 1
    thank you, i checked that but the error does not seam to return a clear error code that allows to determine the source of the error... – alexandre Apr 11 '17 at 12:11
  • In addition to that, this doesn't help openning OS parameters – alexandre Apr 11 '17 at 12:12
  • To open the settings in ios, you can use : `Linking.openURL('app-settings:')`. More info [here](http://stackoverflow.com/questions/39081688/open-settings-app-from-another-app-in-ios-react-native) – G. Hamaide Apr 11 '17 at 12:14
  • @alexandre you can also us the library I added in my answer. – G. Hamaide Apr 11 '17 at 12:17
  • Yeah, cool thanks ! And do you have any idea how to sort error types ? like error because, location is not authorized for your app, error because location is off, error because couldn't retrive location, error timeout... – alexandre Apr 11 '17 at 12:23
  • You have a list of status [here](https://github.com/timfpark/react-native-location#locationgetauthorizationstatus) that already gives you some info on the reason you could not get the location. It's all the info you'll get I fear. – G. Hamaide Apr 11 '17 at 12:38