Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Geolocation 'enableHighAccuracy' on Android always times out #7495

Closed
Findiglay opened this issue May 10, 2016 · 239 comments
Closed

Geolocation 'enableHighAccuracy' on Android always times out #7495

Findiglay opened this issue May 10, 2016 · 239 comments
Labels
API: Geolocation Bug Help Wanted :octocat: Issues ideal for external contributors. Platform: Android Android applications. Ran Commands One of our bots successfully processed a command. Resolution: Locked This issue was locked by the bot.

Comments

@Findiglay
Copy link

Findiglay commented May 10, 2016

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?

@Findiglay
Copy link
Author

Findiglay commented May 11, 2016

@acapop77 Forgot to mention also that permissions are enabled using:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

@acapop77
Copy link

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:

  1. In android emulator device go to "Settings > Location" (like you would on the actual device) and set Mode to High accuracy.
  2. Go back to application.
  3. 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.
  4. 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.

@rossthedevigner
Copy link

rossthedevigner commented May 12, 2016

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

@Findiglay
Copy link
Author

@acapop77 @rossthedevigner I'm also building directly to a Samsung S6 device.

@rossthedevigner
Copy link

@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.

@Daniel-ltw
Copy link

Anyone having navigator.geolocation == undefined?

I am currently having this issue on my device.

@Findiglay
Copy link
Author

Findiglay commented May 15, 2016

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?

@shtefanntz
Copy link
Contributor

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? :(

@EugeneLiang
Copy link

EugeneLiang commented May 18, 2016

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.

@shtefanntz
Copy link
Contributor

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

@RoBYCoNTe
Copy link

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.

@Sean-Snow

This comment has been minimized.

@kiatweitan
Copy link

kiatweitan commented Jun 27, 2016

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}
                );  

@chinloongtan
Copy link

chinloongtan commented Jun 27, 2016

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.

@alexHlebnikov
Copy link

alexHlebnikov commented Jun 30, 2016

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.

@alexHlebnikov
Copy link

Hey guys, I've found temporary fix - this package https://github.com/lucasferreira/react-native-android-permissions

@JonathanWi
Copy link

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!

@sattaman
Copy link
Contributor

sattaman commented Aug 8, 2016

+1

@sattaman
Copy link
Contributor

sattaman commented Aug 8, 2016

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

@willoughby
Copy link

willoughby commented Aug 10, 2016

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 );

@aelsabbahy
Copy link

I'm assuming this is related to: https://productpains.com/post/react-native/add-android-m-permissions-model-support

right?

@willoughby
Copy link

No that was for the location timeout issue

@sattaman
Copy link
Contributor

Yes I had just discovered removing the 3rd argument seems to be a fix.

We haven't fully device tested yet but it seems that android version 22, with device mode set to either high accuracy or device only, will cause the cause the location to timeout, when the 3rd argument is included regardless of settings. It seems to not affect android version 23.

@JonoH
Copy link

JonoH commented Aug 11, 2016

Same experience my side, except android version 23 only works with the 3rd argument when enableHighAccuracy is set to false (true causes location to timeout). Also tested on android version 22, 21, 20 & 19 and it seems the presence of the 3rd argument causes a timeout in all cases. Removing that argument seems to be the only reliable fix.

@DWboutin
Copy link

DWboutin commented Aug 21, 2016

Removing the 3rd argument just make me have the popup of "Location request timed out" instantly instead of waiting the until the timeout is done. Setting enableHighAccuracy to false, doesn't work either.

On my emulator, the Location is only available when i open the emulator settings and send a location (check the screenshot).

Is there a way to get it automatically?

screen shot 2016-08-21 at 6 42 52 pm

@alexHlebnikov
Copy link

In my case setting enableHighAccuracy to false solved the problem.

@dlvx
Copy link

dlvx commented Sep 9, 2016

Having the same problem here, both on emulator and on device, and tried with both true and false for enableHighAccuracy

@page-fault-in-nonpaged-area

I just outright get "undefined is not an object (evaluating 'navigator.geolocation.getCurrentPosition')"

I tried checking if(navigator), that returned true,
but if(navigator.geolocation) returned false.

Also, doing this on device on Android 5.1 OnePlus One. Developer mode enabled, AndroidManifest permissions included, mock locations on/off, everything.

@vishalTechnoFreek
Copy link

Hi I was facing same issue "request time out " I have tried every possible solution as given above those could not work for me but recently I found one module which overcomes this problem.
https://www.npmjs.com/package/react-native-geolocation-service
This worked for me .....

@aMarCruz
Copy link

@vishalTechnoFreek , also exists react-native-cross-geolocation.

It is fully compatible with the current RN geolocation, but using the new Google API. With this API you don't worry which mode to use, the service automatically detects the best option.

Another advantage is that, when RN corrects this problem, you can remove it without affecting your app, if you wish.

@nahuelhds
Copy link

nahuelhds commented Nov 20, 2018

For my case what mentioned @manurana about (downloading and) opening the Google Maps app and then go back to Expo did the trick.

I'm working with Genymotion and Android 5.

Also, for Genymotion users, for properly reading the position the Genymotion's map window must be opened; otherwise it'll work but with the default Geny's location

@iamitkhatkar
Copy link

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.

lifesaver 👍 this worked for me.

@jmlavoier
Copy link

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.

I'm facing the same problem, but setting enableHighAccuracy to false doesn't work for me. So this solution that seems perfect doesn't work for me.

@sreevisakh
Copy link

I faced similar problem with multiple requests (continuously selecting use current location ) I have added retry for the location request. not the exact problem but may be will help somebody.

import retry from 'retry'
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
  const operation = retry.operation({ retries: 2 });
  operation.attempt(() => {
    navigator.geolocation.getCurrentPosition(
      (response) => { resolve(response) },
      (error) => {
        if (operation.retry(error)) {
          return;
        }
        reject(error)
     },
     { timeout: 2000, enableHighAccuracy: true, maximumAge: 100 },
   );
 })
} else {
  reject(new Error('Location access denied'))
}

@nikhillahoti
Copy link

nikhillahoti commented Jan 27, 2019

https://www.npmjs.com/package/react-native-geolocation-service
This package solved the problem for me.
Almost all the above suggestions did not work for me.

@hramos hramos removed the Bug Report label Feb 6, 2019
@GersonM
Copy link

GersonM commented Feb 11, 2019

Hi, Guest I solve the mistery, what happend is that when we ask for the location the first time and use "maximunAge" we dont have a previous location cached so, there are a bug, be sure that the very first time when ask for location is with no "maximunAge"

@Michaelvons
Copy link

the simple answer is to step out of where you are and go to open space then try again.

Its simple science.

GPS services are affected by thick walls and cars.

This worked for me. No need to edit anything !!!

@parnekov
Copy link

parnekov commented Mar 2, 2019

In my case remove maximumAge solved the problem.

Thank you very much! Work!

@akhilsanker
Copy link

Hi,
None of the solutions works for me.
I use navigator.geolocation, using only {enableHighAccuracy: false} as the third parameter.
In iOS it works fine and in android its not working in certain devices.
I have checked in Android 7, 8, 9. Its not working in Lenovo phone with Android 7. But works with other devices with Android 7.

Do anyone have any workaround to solve this issue.
Any suggestion is appreciable.

Thanks

@reyalpsirc
Copy link
Contributor

@akhilsanker maybe try a library like react-native-fused-location

@akhilsanker
Copy link

Hi @reyalpsirc ,

Thanks for the reply. Really appreciate for your time.
There is no issue in iOS and works fine in most of the android devices. I have limited time constrain and it will be hard to use a new library, as you suggested.
Do you have any other workaround to solve this issue?
Thanks for your time.

Thank you.

@reyalpsirc
Copy link
Contributor

Sorry @akhilsanker, all I did was to use react native's Geolocation for iOS and FusedLocation for Android.

@imansalhi
Copy link

setting maximumAge:0 worked for me:
{ enableHighAccuracy: true, timeout: 60000, maximumAge:0 }
maybe @GersonM is right ...

@dulmandakh
Copy link
Contributor

Geolocation module is removed from RN in master, and won't be available in 0.60. Please use https://github.com/react-native-community/react-native-geolocation and report issues there. Thank you

@ollyde
Copy link

ollyde commented Sep 3, 2019

It's 2019, tried 3 different locational libraries none of them work for Android. Amazing.

@oakis
Copy link

oakis commented Sep 4, 2019

It's 2019, tried 3 different locational libraries none of them work for Android. Amazing.

react-native-geolocation-service is working fine.

If a package is not working, create an issue ticket on that repo. Issues won't fix themselves, join the community.

@eliataylor
Copy link

eliataylor commented Sep 4, 2019

see iwangsyah's comment. I needed both the <permission> and <uses-permission> meta tags

@erickcouto
Copy link

https://www.npmjs.com/package/react-native-geolocation-service
This package solved the problem for me.
Almost all the above suggestions did not work for me.

Thank you!

@facebook facebook locked as resolved and limited conversation to collaborators Apr 10, 2020
@react-native-bot react-native-bot added the Resolution: Locked This issue was locked by the bot. label Apr 10, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
API: Geolocation Bug Help Wanted :octocat: Issues ideal for external contributors. Platform: Android Android applications. Ran Commands One of our bots successfully processed a command. Resolution: Locked This issue was locked by the bot.
Projects
None yet
Development

No branches or pull requests