Search code examples
androidreact-nativegpsandroid-gps

Device GPS configuration in react-native


I'm developing a react-native app and I want to check that if the GPS of device is on do something and if the device's GPS is off, tell the user and request him to turn it on. So my question is:

  • How can I check that the GPS of device is on?
  • How can I redirect the user to GPS setting page?

I want to do these without adding any new package to project.


Solution

  • My problem is solved. I should use the error code of the getCurrentPosition function. If error code equals 1, it means that the GPS of device is off.

    navigator.geolocation.getCurrentPosition(
          (position) => {
            ...
          },
          (error) => {
            if (error.code === 1) {
              // gps is off
            }
            ...
          },
          {enableHighAccuracy: false, timeout: 10000},
        )