react-native-geolocation: Function getCurrentPosition always returns timeout error on Android

Platforms

Android

Versions

  • Android: 9
  • react-native-geolocation: 1.4.2
  • react-native: 0.61.1
  • react: 16.9.0
  • enableHermes: true

Description

Function Geolocation.getCurrentPosition always returns error on Android

Location request timed out

{ "PERMISSION_DENIED": 1, "POSITION_UNAVAILABLE": 2, "TIMEOUT": 3, "code": 3, "message": "Location request timed out" }

I’ve tried to add timeout and maximumAge options but it doesn’t help me.

return new Promise(resolve => {
    Geolocation.getCurrentPosition(
      position => {
        const { latitude, longitude } = position.coords;

        resolve({ latitude, longitude });
      },
      err => resolve(err),
      {
        enableHighAccuracy: true,
        timeout: 100,
        maximumAge: 0,
      }
    );
  });

Reproducible Demo

About this issue

  • Original URL
  • State: open
  • Created 5 years ago
  • Reactions: 30
  • Comments: 29

Most upvoted comments

~Getting same issue after upgrading to react native 0.61. Not passing any configuration, simply calling Geolocation.getCurrentPosition((info): void => { /* do something*/ });~

Update: Added <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> to the AndroidManifest.xml and the issue went away 👍

It’s probably because you have enableHighAccuracy set to true. When you’re in a building, the phone fails to accurately locate you and times out. What I did is:

  • First fetch the user position in high accuracy and a 2 seconds timeout. (2000)
  • Catch the exception, and retry with high accuracy disabled (enableHighAccuracy: false)

Remove completely maximumAge param. It worked for me.

Hey guys, I have also received this issue lately. When you add a timeout in the config like …,{timeout: 10000} you WILL receive this error just ten seconds later. I thought it would give it more time to return successfully but no…

@hugopretorius914 thanks man, that helped me! But I didn’t do exactly what you said.

What I noticed is when I set this way below, it doesn’t work at all:

{
    enableHighAccuracy: false,
    timeout: 20000
}

But if I set this way, it works!

{
    enableHighAccuracy: true,
    timeout: 20000
}

This way works as well, like you said:

{
      enableHighAccuracy: false,   //or true
}

I don’t know why it’s working this way, but it works! I hope this can help somebody else.

By the way, I’m not using this lib, instead the one I have installed in my project is: https://github.com/Agontuk/react-native-geolocation-service

“react-native-geolocation-service”: ^5.1.1 “react”: “17.0.1”, “react-native”: “0.63.4”, Windows 10

I am also getting this error lately. I am using the following code:

      initialPosition => {
        // Alert.alert(JSON.stringify(initialPosition))
        this.savePosition(initialPosition);
      },
      error =>
        console.log("The location could not be loaded because ", error.message),
      { enableHighAccuracy: false, timeout: 20000, maximumAge: 1000 }
    );```

Hey @ravi-pandit14 as mentioned in previous comments react-native-geolocation-service works, it has a good readme with examples

@espipj there is problem with geolocation when u testing on some mobile phones, im getting(as mentioned above) permission denied error, but not in all mobile phones

{ “PERMISSION_DENIED”: 1, “POSITION_UNAVAILABLE”: 2, “TIMEOUT”: 3, “code”: 3, “message”: “Location request timed out” }

I’m getting same issue if I set maximumAge with react native version 0.61.2

Getting the same error after upgrading to sdk 28

Getting the same error, is there another plugin that works better?

I’m facing same issue with my physical device. Works fine in emulator.

edit: i applied @espipj 's suggestion and its working now.

Hey guys, I have also received this issue lately. When you add a timeout in the config like …,{timeout: 10000} you WILL receive this error just ten seconds later. I thought it would give it more time to return successfully but no…

Same error here! I suggest to use https://github.com/Agontuk/react-native-geolocation-service which uses react-native-geolocation under the hood on iOS but works flawlessly on Android. 😄 @azhaubassar

The solution of @Temirtator works for me. Thanks!

My problem is similar to some extent. I have set the enableHighAccuracy to True. Sometimes it works fine but other times it returns Request Time Out response. I have increased the timeout limit to 150000 ms and maximumAge to 0, I have also added the FIN_Location permission in android manifest file. I think either the API is unstable or the surrounding factor is the issue. If i set the high accuracy to false then accuracy drops.

is there any solution for this case?

I have the same error, any solutions? 😦