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

Open
Findiglay opened this Issue May 10, 2016 · 216 comments

Comments

Projects
None yet
@Findiglay

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

This comment has been minimized.

Show comment
Hide comment
@Findiglay

Findiglay May 11, 2016

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

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

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

This comment has been minimized.

Show comment
Hide comment
@acapop77

acapop77 May 12, 2016

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.

acapop77 commented May 12, 2016

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

This comment has been minimized.

Show comment
Hide comment
@rossthedevigner

rossthedevigner 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

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

This comment has been minimized.

Show comment
Hide comment
@Findiglay

Findiglay May 13, 2016

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

Findiglay commented May 13, 2016

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

@rossthedevigner

This comment has been minimized.

Show comment
Hide comment
@rossthedevigner

rossthedevigner May 13, 2016

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

rossthedevigner commented May 13, 2016

@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

This comment has been minimized.

Show comment
Hide comment
@Daniel-ltw

Daniel-ltw May 15, 2016

Anyone having navigator.geolocation == undefined?

I am currently having this issue on my device.

Daniel-ltw commented May 15, 2016

Anyone having navigator.geolocation == undefined?

I am currently having this issue on my device.

@Findiglay

This comment has been minimized.

Show comment
Hide comment
@Findiglay

Findiglay 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?

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

This comment has been minimized.

Show comment
Hide comment
@shtefanntz

shtefanntz May 17, 2016

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

Contributor

shtefanntz commented May 17, 2016

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

This comment has been minimized.

Show comment
Hide comment
@EugeneLiang

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

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

This comment has been minimized.

Show comment
Hide comment
@shtefanntz

shtefanntz May 18, 2016

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

Contributor

shtefanntz commented May 18, 2016

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

This comment has been minimized.

Show comment
Hide comment
@RoBYCoNTe

RoBYCoNTe May 20, 2016

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.

RoBYCoNTe commented May 20, 2016

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 was marked as off-topic.

Show comment
Hide comment
@Sean-Snow

Sean-Snow commented May 31, 2016

+1

@kiatweitan

This comment has been minimized.

Show comment
Hide comment
@kiatweitan

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

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

This comment has been minimized.

Show comment
Hide comment
@chinloongtan

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

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

This comment has been minimized.

Show comment
Hide comment
@alexHlebnikov

alexHlebnikov 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 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

This comment has been minimized.

Show comment
Hide comment
@alexHlebnikov

alexHlebnikov commented Jun 30, 2016

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

@JonathanWi

This comment has been minimized.

Show comment
Hide comment
@JonathanWi

JonathanWi Jul 24, 2016

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!

JonathanWi commented Jul 24, 2016

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

This comment has been minimized.

Show comment
Hide comment
@sattaman

sattaman commented Aug 8, 2016

+1

@sattaman

This comment has been minimized.

Show comment
Hide comment
@sattaman

sattaman 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

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

This comment has been minimized.

Show comment
Hide comment
@willoughby

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

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

This comment has been minimized.

Show comment
Hide comment

aelsabbahy commented Aug 10, 2016

@willoughby

This comment has been minimized.

Show comment
Hide comment
@willoughby

willoughby Aug 11, 2016

No that was for the location timeout issue

willoughby commented Aug 11, 2016

No that was for the location timeout issue

@sattaman

This comment has been minimized.

Show comment
Hide comment
@sattaman

sattaman Aug 11, 2016

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.

sattaman commented Aug 11, 2016

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

This comment has been minimized.

Show comment
Hide comment
@JonoH

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

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

This comment has been minimized.

Show comment
Hide comment
@DWboutin

DWboutin 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

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

This comment has been minimized.

Show comment
Hide comment
@alexHlebnikov

alexHlebnikov Aug 22, 2016

In my case setting enableHighAccuracy to false solved the problem.

alexHlebnikov commented Aug 22, 2016

In my case setting enableHighAccuracy to false solved the problem.

@dlvx

This comment has been minimized.

Show comment
Hide comment
@dlvx

dlvx Sep 9, 2016

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

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

@Ascendance

This comment has been minimized.

Show comment
Hide comment
@Ascendance

Ascendance Sep 10, 2016

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.

Ascendance commented Sep 10, 2016

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.

@Ascendance

This comment has been minimized.

Show comment
Hide comment
@Ascendance

Ascendance Sep 10, 2016

also, it didn't seem to ask me for location permissions 🤔despite me deleting the app on device and the output folder on my mac.

Ascendance commented Sep 10, 2016

also, it didn't seem to ask me for location permissions 🤔despite me deleting the app on device and the output folder on my mac.

@Ascendance

This comment has been minimized.

Show comment
Hide comment
@Ascendance

Ascendance Sep 10, 2016

I can't actually debug remotely either since I'm using React Realm and that thing has HUGE problems that screw the chrome remote debugger inside-out.

Ascendance commented Sep 10, 2016

I can't actually debug remotely either since I'm using React Realm and that thing has HUGE problems that screw the chrome remote debugger inside-out.

@L3V147H4N

This comment has been minimized.

Show comment
Hide comment
@L3V147H4N

L3V147H4N Sep 14, 2016

I have a similar Issue, may app doesn't always timesout BUT it does it a lot, when other geolocation apps work just fine everytime the one with RN has very poor geolocation.
Im using RN 0.32 but I had the same issue with every version, for some reason geolocation in RN es very weird and poor (at least on Android)

L3V147H4N commented Sep 14, 2016

I have a similar Issue, may app doesn't always timesout BUT it does it a lot, when other geolocation apps work just fine everytime the one with RN has very poor geolocation.
Im using RN 0.32 but I had the same issue with every version, for some reason geolocation in RN es very weird and poor (at least on Android)

@leo7r

This comment has been minimized.

Show comment
Hide comment
@leo7r

leo7r Sep 15, 2016

Same issue here, testing on real device.

leo7r commented Sep 15, 2016

Same issue here, testing on real device.

@julien-rodrigues

This comment has been minimized.

Show comment
Hide comment
@julien-rodrigues

julien-rodrigues Sep 21, 2016

Running into this and this is really annoying :/

julien-rodrigues commented Sep 21, 2016

Running into this and this is really annoying :/

@lwinkyawmyat

This comment has been minimized.

Show comment
Hide comment
@lwinkyawmyat

lwinkyawmyat Sep 23, 2016

Collaborator

I'm having the same issue on Android 6.0.

Collaborator

lwinkyawmyat commented Sep 23, 2016

I'm having the same issue on Android 6.0.

@luisfuertes

This comment was marked as off-topic.

Show comment
Hide comment
@luisfuertes

luisfuertes commented Sep 23, 2016

+1

@sattaman

This comment has been minimized.

Show comment
Hide comment
@sattaman

sattaman Sep 23, 2016

I found a solution was to not add a 3rd parameter , and to use the library linked above (https://github.com/lucasferreira/react-native-android-permissions) on android version >=23 to request android M permissions.

sattaman commented Sep 23, 2016

I found a solution was to not add a 3rd parameter , and to use the library linked above (https://github.com/lucasferreira/react-native-android-permissions) on android version >=23 to request android M permissions.

@sattaman

This comment has been minimized.

Show comment
Hide comment
@sattaman

sattaman Sep 23, 2016

Just spotted PermissionsAndroid has now been added to RN. So use this to request if on android >= 23

https://facebook.github.io/react-native/docs/permissionsandroid.html

sattaman commented Sep 23, 2016

Just spotted PermissionsAndroid has now been added to RN. So use this to request if on android >= 23

https://facebook.github.io/react-native/docs/permissionsandroid.html

@luisfuertes

This comment has been minimized.

Show comment
Hide comment
@luisfuertes

luisfuertes Sep 23, 2016

But i have Android 5.1.1 on my device. On this v. of android, Geolocation must works fine, no?

luisfuertes commented Sep 23, 2016

But i have Android 5.1.1 on my device. On this v. of android, Geolocation must works fine, no?

@globetro

This comment has been minimized.

Show comment
Hide comment
@globetro

globetro Jan 23, 2018

I've been experiencing this issue for a while and the only "solution" that works consistently is to disable "enableHighAccuracy". The issue with this is that it then uses your network instead of GPS to determine your location, which loses a ton of accuracy.

Many of the other solutions seem to work at first, but I've found it's usually due to using a cached position and if you restart your phone and launch your app, it would still fail.

Today, I found this package that uses Google's fused locations API for Android locations and falls back to the default React Native implementation for iOS (I'm not the author of this package, but kudos to him for creating it):

https://www.npmjs.com/package/react-native-geolocation-service

I've only tested it for a bit so far, but it seems to be working well.

globetro commented Jan 23, 2018

I've been experiencing this issue for a while and the only "solution" that works consistently is to disable "enableHighAccuracy". The issue with this is that it then uses your network instead of GPS to determine your location, which loses a ton of accuracy.

Many of the other solutions seem to work at first, but I've found it's usually due to using a cached position and if you restart your phone and launch your app, it would still fail.

Today, I found this package that uses Google's fused locations API for Android locations and falls back to the default React Native implementation for iOS (I'm not the author of this package, but kudos to him for creating it):

https://www.npmjs.com/package/react-native-geolocation-service

I've only tested it for a bit so far, but it seems to be working well.

@ujwal-setlur

This comment has been minimized.

Show comment
Hide comment
@ujwal-setlur

ujwal-setlur Jan 23, 2018

@globetro Thanks! I checked this out as well, and seems to work fine!

ujwal-setlur commented Jan 23, 2018

@globetro Thanks! I checked this out as well, and seems to work fine!

@hoorsa

This comment has been minimized.

Show comment
Hide comment
@hoorsa

hoorsa commented Jan 25, 2018

@globetro Thank You.
react-native-geolocation-service is the best solution.

@oakis

This comment has been minimized.

Show comment
Hide comment
@oakis

oakis Jan 31, 2018

@globetro I also had success with this plugin. This should be merged in to react-native as the default geolocation functionality!

oakis commented Jan 31, 2018

@globetro I also had success with this plugin. This should be merged in to react-native as the default geolocation functionality!

@antonsivogrivov

This comment has been minimized.

Show comment
Hide comment
@antonsivogrivov

antonsivogrivov Feb 9, 2018

react-native-geolocation-service not helps me :(

antonsivogrivov commented Feb 9, 2018

react-native-geolocation-service not helps me :(

@Germanaz0

This comment has been minimized.

Show comment
Hide comment
@Germanaz0

Germanaz0 Feb 9, 2018

Thanks @globetro the package **react-native-geolocation-service ** worked perfectly, it shouls be nice to add this as core component on react native.

Germanaz0 commented Feb 9, 2018

Thanks @globetro the package **react-native-geolocation-service ** worked perfectly, it shouls be nice to add this as core component on react native.

rodolphefauquez pushed a commit to Clintal/react-native-picker that referenced this issue Feb 16, 2018

Merged in CLT-711_geolocalisation_android (pull request #59)
CLT-711 geolocalisation android

* - The problem was from High_Accuracy in Android. Removed the 3rd parameter in getCurrentPosition. Now working, even if the user location type is high_accuracy in his android parameters.
    - Also added alert messages to the user: Because if he allow the permission, but his location stay disabled, the application provide no feedback to the user.

* - add little library who can handle (with playservices) the high_precision issue for Android
    facebook/react-native#7495
    - added some prompt message to the user to let him know the problem in case of failure
    - added requestPermissionLocation (e.g if the permission was removed after install)
    - Removed some not used imports in ChatSaga

* removed Alerts

* Documented error 5 and added github link in comment

Approved-by: Aaron Lee <aaron@onefatgiraffe.com>
@dreallday

This comment has been minimized.

Show comment
Hide comment
@dreallday

dreallday Feb 16, 2018

You really want to have two location listeners(which is expensive), one that does highAccuracy and one that doesn't.

then you want to compare which is more accurate and newer than your 'current' location.


This is a 'temporary' solution, this really should be implemented on RN side so it uses both COARSE and FINE permissions if highAccuracy is enabled.

dreallday commented Feb 16, 2018

You really want to have two location listeners(which is expensive), one that does highAccuracy and one that doesn't.

then you want to compare which is more accurate and newer than your 'current' location.


This is a 'temporary' solution, this really should be implemented on RN side so it uses both COARSE and FINE permissions if highAccuracy is enabled.

@a3diti

This comment has been minimized.

Show comment
Hide comment
@a3diti

a3diti Mar 9, 2018

I fixed this problem, here is the component i created using a promise:
https://gist.github.com/a3diti/5466f7309752cbb2e63ecefff6365bb9
In android i removed maximumAge, i kept enableHighAccuracy to true, and added a permission check from RN PermissionsAndroid component.

a3diti commented Mar 9, 2018

I fixed this problem, here is the component i created using a promise:
https://gist.github.com/a3diti/5466f7309752cbb2e63ecefff6365bb9
In android i removed maximumAge, i kept enableHighAccuracy to true, and added a permission check from RN PermissionsAndroid component.

@oakis

This comment has been minimized.

Show comment
Hide comment
@oakis

oakis Mar 9, 2018

@a3diti Have you tested it out in the real world? Location could be cached.

oakis commented Mar 9, 2018

@a3diti Have you tested it out in the real world? Location could be cached.

@globetro

This comment has been minimized.

Show comment
Hide comment
@globetro

globetro Mar 9, 2018

@a3diti Unless they fixed something in the latest react-native release, that will not work at all times on Android. Try launching that on a real Android device after a reboot. It will likely take over 20s to get a position update, if not just timeout.

globetro commented Mar 9, 2018

@a3diti Unless they fixed something in the latest react-native release, that will not work at all times on Android. Try launching that on a real Android device after a reboot. It will likely take over 20s to get a position update, if not just timeout.

@Germanaz0

This comment has been minimized.

Show comment
Hide comment
@Germanaz0

Germanaz0 commented Mar 9, 2018

Why don't merge the code used in here https://www.npmjs.com/package/react-native-geolocation-service into core ?

@vishalgadhiya91

This comment has been minimized.

Show comment
Hide comment
@vishalgadhiya91

vishalgadhiya91 Mar 10, 2018

I facing same "Request time out issue",After R & D i try to "Reboot" device then after i getting location & its work fine.

Please check my code.

componentDidMount = () => {
navigator.geolocation.getCurrentPosition(
(position) => {
const initialPosition = JSON.stringify(position);
this.setState({ initialPosition });
},
(error) => alert(error.message),
{ enableHighAccuracy: false, timeout: 20000, maximumAge: 1000, distanceFilter: 1 }

  );
  this.watchID = navigator.geolocation.watchPosition((position) => {
     const lastPosition = JSON.stringify(position);
     this.setState({ lastPosition });
     Alert.alert("Location change",lastPosition);

  });

}

vishalgadhiya91 commented Mar 10, 2018

I facing same "Request time out issue",After R & D i try to "Reboot" device then after i getting location & its work fine.

Please check my code.

componentDidMount = () => {
navigator.geolocation.getCurrentPosition(
(position) => {
const initialPosition = JSON.stringify(position);
this.setState({ initialPosition });
},
(error) => alert(error.message),
{ enableHighAccuracy: false, timeout: 20000, maximumAge: 1000, distanceFilter: 1 }

  );
  this.watchID = navigator.geolocation.watchPosition((position) => {
     const lastPosition = JSON.stringify(position);
     this.setState({ lastPosition });
     Alert.alert("Location change",lastPosition);

  });

}

@ujwal-setlur

This comment has been minimized.

Show comment
Hide comment
@ujwal-setlur

ujwal-setlur Mar 10, 2018

Any reason why people are still struggling with this and not using

https://www.npmjs.com/package/react-native-geolocation-service

as previously suggested? It works perfectly for me.

Is it not working for others?

ujwal-setlur commented Mar 10, 2018

Any reason why people are still struggling with this and not using

https://www.npmjs.com/package/react-native-geolocation-service

as previously suggested? It works perfectly for me.

Is it not working for others?

@ZStone20281

This comment has been minimized.

Show comment
Hide comment
@ZStone20281

ZStone20281 Mar 21, 2018

Added this to the AndroidManifest.xml file and it works!

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

ZStone20281 commented Mar 21, 2018

Added this to the AndroidManifest.xml file and it works!

  <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
  <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
@Patrick-Bayer

This comment has been minimized.

Show comment
Hide comment
@Patrick-Bayer

Patrick-Bayer Apr 4, 2018

Hey guys,

i tried to use react-native-geolocation-service as suggested by @globetro but it causes my app to crash instantly. Any ideas?

Patrick-Bayer commented Apr 4, 2018

Hey guys,

i tried to use react-native-geolocation-service as suggested by @globetro but it causes my app to crash instantly. Any ideas?

@oakis

This comment has been minimized.

Show comment
Hide comment
@oakis

oakis Apr 4, 2018

@Patrick-Bayer I'm using it on my app on Google Play where it works, but my development version is now crashing instantly for an unknown reason. It seems to be react-native-geolocation-service that is causing the crash for me as well.

Fatal Exception: java.lang.VerifyError: Verifier rejected class com.google.android.gms.location.FusedLocationProviderClient: void com.google.android.gms.location.FusedLocationProviderClient.<init>(android.app.Activity) failed to verify: void com.google.android.gms.location.FusedLocationProviderClient.<init>(android.app.Activity): [0x8] register v2 has type Precise Reference: com.google.android.gms.common.api.internal.zzg but expected Precise Reference: com.google.android.gms.common.api.internal.zzcz

Let me know if you find a solution.

oakis commented Apr 4, 2018

@Patrick-Bayer I'm using it on my app on Google Play where it works, but my development version is now crashing instantly for an unknown reason. It seems to be react-native-geolocation-service that is causing the crash for me as well.

Fatal Exception: java.lang.VerifyError: Verifier rejected class com.google.android.gms.location.FusedLocationProviderClient: void com.google.android.gms.location.FusedLocationProviderClient.<init>(android.app.Activity) failed to verify: void com.google.android.gms.location.FusedLocationProviderClient.<init>(android.app.Activity): [0x8] register v2 has type Precise Reference: com.google.android.gms.common.api.internal.zzg but expected Precise Reference: com.google.android.gms.common.api.internal.zzcz

Let me know if you find a solution.

@Patrick-Bayer

This comment has been minimized.

Show comment
Hide comment
@Patrick-Bayer

Patrick-Bayer Apr 4, 2018

@oakis The following steps worked for me:

  1. Update Gradle and change the version in
dependencies {
        classpath 'com.android.tools.build:gradle:3.1.0'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }

  1. Add "google()" to the repositories in your android/build.gradle

Patrick-Bayer commented Apr 4, 2018

@oakis The following steps worked for me:

  1. Update Gradle and change the version in
dependencies {
        classpath 'com.android.tools.build:gradle:3.1.0'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }

  1. Add "google()" to the repositories in your android/build.gradle
@SirPhemmiey

This comment has been minimized.

Show comment
Hide comment
@SirPhemmiey

SirPhemmiey Apr 23, 2018

Sometimes even after you add <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> to your AndroidManifest.xml it still might not work out.
Quick Workaround

  1. You could explicitly ask for the user's location info when they mount the first screen of your app like so
    try { const granted = await PermissionsAndroid.request( PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION ) if (granted == PermissionsAndroid.RESULTS.GRANTED) { // WHATEVER WAY YOU WANT THEM TO KNOW THAT THEY HAVE GRANTED THE PERMISSION //MAY BE USING BASIC ALERT OR TOASTANDROID } else { //SAME AS ABOVE } } catch (error) { //catch the error alert(error); }
  2. Never set enableHighAccuracy to be false, instead remove maximumAge and increase your timeOut. Something like {enableHighAccuracy: true, timeout: 100000,} would do.

I hope this will help someone.

SirPhemmiey commented Apr 23, 2018

Sometimes even after you add <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> to your AndroidManifest.xml it still might not work out.
Quick Workaround

  1. You could explicitly ask for the user's location info when they mount the first screen of your app like so
    try { const granted = await PermissionsAndroid.request( PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION ) if (granted == PermissionsAndroid.RESULTS.GRANTED) { // WHATEVER WAY YOU WANT THEM TO KNOW THAT THEY HAVE GRANTED THE PERMISSION //MAY BE USING BASIC ALERT OR TOASTANDROID } else { //SAME AS ABOVE } } catch (error) { //catch the error alert(error); }
  2. Never set enableHighAccuracy to be false, instead remove maximumAge and increase your timeOut. Something like {enableHighAccuracy: true, timeout: 100000,} would do.

I hope this will help someone.

@xMaarf

This comment has been minimized.

Show comment
Hide comment
@xMaarf

xMaarf Apr 26, 2018

For me setting enableHighAccuracy to true works on API 23, although I need to set to false on API 26.
(it's the only ones that I'm testing on)

EDIT: I ended up installing react-native-geolocation-service and that solved my problems 🥁

xMaarf commented Apr 26, 2018

For me setting enableHighAccuracy to true works on API 23, although I need to set to false on API 26.
(it's the only ones that I'm testing on)

EDIT: I ended up installing react-native-geolocation-service and that solved my problems 🥁

@hkurokawa

This comment has been minimized.

Show comment
Hide comment
@hkurokawa

hkurokawa May 25, 2018

Hi,

I had the same issue and here is another solution.

I added the below uses-feature tag in AndroidManifest.xml and it seems the app works fine with highAccuracy: true

<uses-feature android:name="android.hardware.location.gps" />

It seems if you set highAccuracy: high, the React Native uses GPS_PROVIDER instead of NETWORK_PROVIDER. And as Android Developers document says, an app targeting API 21 or higher must include this tag in AndroidManifest.xml to use GPS_PROVIDER.

Caution: If your app targets Android 5.0 (API level 21) or higher, you must declare that your app uses the android.hardware.location.network or android.hardware.location.gps hardware feature in the manifest file, depending on whether your app receives location updates from NETWORK_PROVIDER or from GPS_PROVIDER. If your app receives location information from either of these location provider sources, you need to declare that the app uses these hardware features in your app manifest. On devices running versions prior to Android 5.0 (API 21), requesting the ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION permission includes an implied request for location hardware features. However, requesting those permissions does not automatically request location hardware features on Android 5.0 (API level 21) and higher.

https://developer.android.com/guide/topics/location/strategies#Permission

I don't know why you do not need to include android.hardware.location.network in AndroidManifest.xml when setting highAccuracy: false which implies LocationManager uses NETWORK_PROVIDER and thus the tag is required according to the document.

Hope this solution works for you.

hkurokawa commented May 25, 2018

Hi,

I had the same issue and here is another solution.

I added the below uses-feature tag in AndroidManifest.xml and it seems the app works fine with highAccuracy: true

<uses-feature android:name="android.hardware.location.gps" />

It seems if you set highAccuracy: high, the React Native uses GPS_PROVIDER instead of NETWORK_PROVIDER. And as Android Developers document says, an app targeting API 21 or higher must include this tag in AndroidManifest.xml to use GPS_PROVIDER.

Caution: If your app targets Android 5.0 (API level 21) or higher, you must declare that your app uses the android.hardware.location.network or android.hardware.location.gps hardware feature in the manifest file, depending on whether your app receives location updates from NETWORK_PROVIDER or from GPS_PROVIDER. If your app receives location information from either of these location provider sources, you need to declare that the app uses these hardware features in your app manifest. On devices running versions prior to Android 5.0 (API 21), requesting the ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION permission includes an implied request for location hardware features. However, requesting those permissions does not automatically request location hardware features on Android 5.0 (API level 21) and higher.

https://developer.android.com/guide/topics/location/strategies#Permission

I don't know why you do not need to include android.hardware.location.network in AndroidManifest.xml when setting highAccuracy: false which implies LocationManager uses NETWORK_PROVIDER and thus the tag is required according to the document.

Hope this solution works for you.

@manurana

This comment has been minimized.

Show comment
Hide comment
@manurana

manurana May 25, 2018

I did some experiments and it seems that the timeout might have to do with whether Google's location services are enabled on the device.
I noticed that when I turn on location services on the device, A alert box asks you "Improve location accurarcy?" with text explaining that Google's location services will run in the background, and also send location data to google. This typically happens on a new device or an emulator, because most of us have already enabled this on our regular devices.

9vgfb

When this is not enabled, the device has to get a fresh location almost everytime you ask for it (controlled by the maximumAge settings). If you have not asked for location for a while, and the timeout value is not enough to get a fresh location, the navigator.geolocation.getCurrentPosition will just return a timeout.

I have also seen this getting resolved by opening google maps on the device once (which again gets a fresh location).

Again, not claiming that this is the right explanation/solution, just adding my observations to this thread

manurana commented May 25, 2018

I did some experiments and it seems that the timeout might have to do with whether Google's location services are enabled on the device.
I noticed that when I turn on location services on the device, A alert box asks you "Improve location accurarcy?" with text explaining that Google's location services will run in the background, and also send location data to google. This typically happens on a new device or an emulator, because most of us have already enabled this on our regular devices.

9vgfb

When this is not enabled, the device has to get a fresh location almost everytime you ask for it (controlled by the maximumAge settings). If you have not asked for location for a while, and the timeout value is not enough to get a fresh location, the navigator.geolocation.getCurrentPosition will just return a timeout.

I have also seen this getting resolved by opening google maps on the device once (which again gets a fresh location).

Again, not claiming that this is the right explanation/solution, just adding my observations to this thread

@reyalpsirc

This comment has been minimized.

Show comment
Hide comment
@reyalpsirc

reyalpsirc Jun 14, 2018

Contributor

I was only able to watch for location changes on Android with react-native-fused-location

Contributor

reyalpsirc commented Jun 14, 2018

I was only able to watch for location changes on Android with react-native-fused-location

@hramos

This comment has been minimized.

Show comment
Hide comment
@hramos

hramos Jun 27, 2018

Contributor

Anyone interested in upstreaming the changes from one of the suggested modules into core?

Contributor

hramos commented Jun 27, 2018

Anyone interested in upstreaming the changes from one of the suggested modules into core?

@aMarCruz

This comment has been minimized.

Show comment
Hide comment
@aMarCruz

aMarCruz Jun 29, 2018

A few days ago I published react-native-cross-geolocation that is compatible with the RN geolocation module, but uses the most efficient Google Location API for Android. Because of that I don't think that my module is a candidate to integrate it into the RN core, but it is available to anyone who wants to use it.

aMarCruz commented Jun 29, 2018

A few days ago I published react-native-cross-geolocation that is compatible with the RN geolocation module, but uses the most efficient Google Location API for Android. Because of that I don't think that my module is a candidate to integrate it into the RN core, but it is available to anyone who wants to use it.

@Shhzdmrz

This comment has been minimized.

Show comment
Hide comment
@Shhzdmrz

Shhzdmrz Jul 4, 2018

react-native-geolocation-service This package is using fused location api and working fine for getting the position or even to watch position. @hramos and @reyalpsirc please give it a try.

Shhzdmrz commented Jul 4, 2018

react-native-geolocation-service This package is using fused location api and working fine for getting the position or even to watch position. @hramos and @reyalpsirc please give it a try.

@dulmandakh

This comment has been minimized.

Show comment
Hide comment
@dulmandakh

dulmandakh Jul 4, 2018

Collaborator

@hramos fused location depends on Google Play Services and I wonder if the license is OK?

Collaborator

dulmandakh commented Jul 4, 2018

@hramos fused location depends on Google Play Services and I wonder if the license is OK?

@nishantWM

This comment has been minimized.

Show comment
Hide comment
@nishantWM

nishantWM Jul 27, 2018

I tried almost everything and turns out it's inconsistent. I have found something which might be useful.

Enabling high accuracy means finding location via gps+network (carrier services). Upon checking the app level permissions, my device had carrier services' location permissions disabled. Enabling it and removing the third parameter from navigator.geolocation.getCurrentPosition() works for me.
Try
-Go to your device's Settings -> Security & location -> Location -> App Level permissions
-Enable location permission for Carrier Services
-Remove the location config (third parameter) from getCurrentPosition
-Restart your device

Hope this helps!

nishantWM commented Jul 27, 2018

I tried almost everything and turns out it's inconsistent. I have found something which might be useful.

Enabling high accuracy means finding location via gps+network (carrier services). Upon checking the app level permissions, my device had carrier services' location permissions disabled. Enabling it and removing the third parameter from navigator.geolocation.getCurrentPosition() works for me.
Try
-Go to your device's Settings -> Security & location -> Location -> App Level permissions
-Enable location permission for Carrier Services
-Remove the location config (third parameter) from getCurrentPosition
-Restart your device

Hope this helps!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment