Geolocation 'enableHighAccuracy' on Android always times out
Developing an Android app, whilst debugging on my Android Device running on 6.1, the geolocation api always returns an error callback with the message Location request timed out.
navigator.geolocation.getCurrentPosition(
(position) => {
console.log(position);
},
(error) => {
console.log(error)
},
{enableHighAccuracy: true, timeout: 20000, maximumAge: 10000}
);
If I toggle enableHighAccuracy option to false, the api works fine. In high accuracy mode, the timeout always takes as long as I specify in the timeout option i.e. 20 seconds.
I've tried running with dev mode turned off.
Should high accuracy mode work in Android?
@acapop77 Forgot to mention also that permissions are enabled using:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
I have same problem on RN 0.25.1, difference is, for me problem persists for high and low accuracy. Finally I found something that makes it work, but is not a solution.
Here is what worked for me - assuming that you use stock android avd - emulator:
- In android emulator device go to "Settings > Location" (like you would on the actual device) and set Mode to High accuracy.
- Go back to application.
- Press "Extended controls" button (represented by "..."). Next in Location section you will see GPS data point data and "Send" button below. Just click on the "Send" and close extended controls menu.
- Reload application.
This works for me - on 'one time only base'. Each time I want to reload app I need to go 3-4 steps again. But at least this way I get Geolocation to work, and do not get timeout error.
I'm experiencing something similar in 0.24. Even though <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> is in my AndroidManifest.xml, I receive the red error stating:
"Looks like the app doesn't have the permission to access location. Add the following line..."
I am not using the emulator and instead building directly to the device.
Edit - this also happens in 0.25.1
@acapop77 @rossthedevigner I'm also building directly to a Samsung S6 device.
@Findiglay Same here. I even tried doing a manual .apk install and I still received the error.
I'm trying a workaround where I loop through the permissions manifest and see if android.permission.ACCESS_FINE_LOCATION is enabled.
Anyone having navigator.geolocation == undefined?
I am currently having this issue on my device.
There are various issues being reported here. I'm not sure that all are relevant to the specific issue with enableHighAccuracy.
I don't have any experience with android development, but I've found that if I edit LocationModule.java to hardcode the LocationOptions boolean highAccuracy = true; and rebuild, then my location will return correctly. I'm not certain whether or not the results are any different from those received with the setting enableHighAccuracy: false in the React-Native geolocation API, but it should be safe to assume this is operating in highAccuracy mode.
Could this mean the bug is happening in the bridge between the Geolocation API & the LocationModule?
I am experiencing the same problem. I did the same as @Findiglay suggested and worked for a few times, but then I got only timeouts.
Any idea why this happens on real devices? :(
Having the same issue. I'm currently running the app on real devices, using RN 0.25.1.
I should add that I have already added the necessary Android permissions to the permissions manifest. But still timeouts.
as a workaround, I enabled 'Allow mock locations' in android settings and installed an app that mocks the gps (look for fake gps in store). At least until this is fixed, development is not blocked
I guys, I was facing the same error but, after few retries with my colleagues, we notices something strange (but normal if you thing about GPS).
If you execute tests "inside your office" using physical device, the GPS locator can be disturbed by external things like walls, or by the simple reason that you are trying to do this operation "indoor" and, generally, this should be executed outdoor.
Well, after this we go outside from our office, we re-execute the tests and the device responds as planned (we don't received the Request timed out error).
My suggestion is: try geolocation using "enableHighAccuracy" set to true, if this fail (in the office this can fail), retry with false, this should work, in my case it did.
Hope this can helps.
+1
Same issue here whether enableHighAccuracy is set to true or false.
It works on GenyMotion emulators but not on any real devices (tested on Samsung, Nexus, Mi).
Enabled permission in AndroidManifest.xml
Code:
navigator.geolocation.getCurrentPosition(
(position) => {
console.log(position);
},
(error) => console.log(new Date(), error),
{enableHighAccuracy: true, timeout: 10000, maximumAge: 3000}
);
setting enableHighAccuracy to either true or false doesn't work for me.
omitting the 3rd argument for getCurrentPosition to use default values from native module works for me.
Same problem here with LGE Nexus 5 (Android 6.0).
On Android 5.1.1 and 4.3 there is no such problem.
In emulator with Android 6.0 there is no such problem.
boolean highAccuracy = false; doesn't works.
RN 0.28.0.
Hey guys, I've found temporary fix - this package https://github.com/lucasferreira/react-native-android-permissions
Any updates or fix for this? Everything works fine on Simulator and pre 5.0 devices, after that only timeout, and I desperately need a fix for an already available app!
+1
This issue is affected by the locationMode setting on the device. When location mode is set to either high accuracy or device only, geolocation times out. It works correctly when location mode is set to battery saving
I've had the same problem, I took a punt and removed the 3rd argument. And its now working.
navigator.geolocation.getCurrentPosition( (position) => { console.log(position); }, (error) => console.log(new Date(), error), {enableHighAccuracy: true, timeout: 10000, maximumAge: 3000} //removed this );