Search code examples
react-nativegeolocation

Unable to get when user dennied activating location - React Native Geolocation Service


I am opening this question and answering it myself to help other users that face the same issue.

Working with a React Native app in Android that uses the package react-native-geolocation-service, when trying to get the user to activate its location (not to allow it, to activate it), I found that the button cancel wasn't returning any errors. Here is the pop up I'm talking about:

enter image description here

The function I was using was the following:

   Geolocation.getCurrentPosition(
        (position) => {
         console.log(position);
        (error) => {
          console.log(error.code, error.message);
        },
        { enableHighAccuracy: true, timeout: 15000, maximumAge: 10000 }
    });

I was completely sure it was correct, because it was a copy from github's repo. But then, the console never logged the error.

If you are facing the same issue, check the answer below.


Solution

  • Turns out, the problem was that a combination of Visual Studio Code pluggins (Prettier and ESLint probably) messed up with my code. The correct code should look like this:

    Geolocation.getCurrentPosition(
          pos => {
            // Do somehting if location activated
          },
          error => {
            console.log(error.code, error.message);
          },
          {enableHighAccuracy: false, timeout: 15000, maximumAge: 10000}
        );
    

    As you can see, all of my previous code was inside the "pos" brackets, meaning that it was only entering if it was successfull. Since the error block could never get reached, I wasn't able to get the negative answer from the user.

    Hope this was of use to anyone.