react-native-geolocation
react-native-geolocation copied to clipboard
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
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)
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 }
);```
I am also facing this issue on Android 9 Phone. Same App working fine on other phones but on this device never able to get currentPosition using geolocation api
Getting the same error, is there another plugin that works better?
Getting the same error after upgrading to sdk 28
~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 👍
I'm getting same issue if I set maximumAge
with react native version 0.61.2
Getting same issue with default and with different options. On emulator it works fine, but on actual device - no. And given this bug exists for about 3 years now guess it wouldn't be fixed.
I have the same error, any solutions? :(
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
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....
I'm facing same issue with my physical device. Works fine in emulator.
edit: i applied @espipj 's suggestion and its working now.
I'm getting the same error in ios: "Unable to fetch location within 10.0s." Changed timeout to 100s, still no luck.
I ran into something similar a while back and noticed that the permissions popup never triggered and the same thing is happing here.
I just added the following to componentDidMount()
Geolocation.setRNConfiguration({ authorizationLevel: 'whenInUse', skipPermissionRequests: false, });
After accepting the permission it works! But now I run into the problem of timing.
I have a timeout of 10 seconds on the getCurrentPosition but if I don't accept the permissions popup quickly enough, the error still triggers.
There doesn't seem to be any convenient way to check if a permission has been accepted besides installing another library to check if accepted, then call getCurrentPosition.
Anyone have a good solution to this one?
@jbinda @jainkuniya @tyronet-sportsbet Any Solution of this issue????
I think this hasn't been fixed yet (in this module) @ravi-pandit14
is there any solution for this case?
@espipj I have tried all of the cases but not success. If geolocation are working fine for you, it would be really helpful if you could provide a sample public repo or complete code.
Hey @ravi-pandit14 as mentioned in previous comments react-native-geolocation-service works, it has a good readme with examples
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" }
Is there any update?
@yurii-oliiarnyk https://github.com/Agontuk/react-native-geolocation-service solves problem, change youre library
Remove completely maximumAge param. It worked for me.
The solution of @Temirtator works for me. Thanks!
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
Remove completely maximumAge param. It worked for me.
It worked for me.
Tengo el mismo problema si configuro
maximumAge
con la versión nativa de reacción0.61.2
24/06/2021 I have this problem, delete the maximumAge (or comment) and run again, this work for me!! Thanz @mauriziocarella
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" } https://github.com/react-native-geolocation/react-native-geolocation/issues/66#issuecomment-579894886
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.
The issue persists on the Android emulator with APIs 29 and 30.
Setting options solve my problem as a workaround.