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

FusedLocation.getFusedLocation() will not return a result if GPS is turned off #4

Closed
jarvisluong opened this issue Aug 2, 2017 · 18 comments

Comments

@jarvisluong
Copy link

Hi!

I used a case with Genymotion emulator and turn off GPS. After the app is loaded. I check and found that the function FusedLocation.getFusedLocation() never return a resolve or reject.

I think that would be a great idea to implement a timeout to know if the GPS is turned off or not.

@MustansirZia
Copy link
Owner

Hey! May I know which location priority setting were you using?

@jarvisluong
Copy link
Author

I'm using FusedLocation.setLocationPriority(FusedLocation.Constants.HIGH_ACCURACY)

@jarvisluong
Copy link
Author

even when I set it to BALANCED, it still behaves the same thing

@MustansirZia
Copy link
Owner

Alright. What I believe is happening is, in the emulator you don't have the hardware for Cell Towers that is supposed to get your location if the GPS is turned off.
(Fused API gets the location from Cell Towers if GPS is turned off so your location request always returns something)
So in the case of an emulator, the Fused API probably hangs.
Really good idea for a time out none the less! Will take a look. :)

@jordanmkoncz
Copy link

I'm running into this issue on the Android emulator too. I have a part of my app relying on getting a response back from the FusedLocation.getFusedLocation() call, so this is a problem for me. I think it would definitely be handy to have a timeout that you can specify, and/or have the getFusedLocation() call reject the promise if it's not able to get the user's location, rather than just hanging and never returning anything.

@MustansirZia
Copy link
Owner

@jordanmkoncz I understand it's an issue and I'm surely gonna work on it when I get time. And I really appreciate the patience. :)

@sumesh1993
Copy link

you can check gps service like this

private boolean checkLocationEnabled(){
       LocationManager lm = (LocationManager)getReactApplicationContext().getSystemService(Context.LOCATION_SERVICE);
       boolean gps_enabled = false;
       try {
           gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
       } catch(Exception ex) {
           Log.e(TAG, ex.toString());
       }

       return gps_enabled;
   }

@sumesh1993
Copy link

@MustansirZia anyupdate on time out ?

@MustansirZia
Copy link
Owner

MustansirZia commented Sep 10, 2017

@jarvisluong @jordanmkoncz @sumesh1993 Okay. Added a check in 0496936 for GPS provider and Network provider and if both of them are disabled (Such as in case of a geny emulator) getFusedLocation would now return an error. Hopefully, this should remove the need for a timeout.
Please provide your feedback.

@jordanmkoncz
Copy link

@MustansirZia thanks, I'll be keen to check that out. Can you update the README to show/explain how to handle this case where getFusedLocation returns an error?

The example code does const location = await FusedLocation.getFusedLocation();, in this situation where it returns an error, would the value of the location variable itself be the error (in which case I assume location.latitude would be null or undefined and you'd need to add a check for this), or would an error be thrown (in which case you'd need to wrap the call to getFusedLocation in a try/catch)?

It would be helpful to have the above clearly explained in the README, and for the example code to show how you would correctly handle the situation where getFusedLocation returns an error.

@MustansirZia
Copy link
Owner

@jordanmkoncz Let me update the README. But for you to state it quickly, an error would be thrown and it would need to be caught in a try/catch, in other words, the promise gets rejected with an appropriate error.

@jordanmkoncz
Copy link

@MustansirZia I just had a chance to do some testing of the new version of the package (0.0.8). I wrapped my code in a try/catch as you suggested, and when I had Location Services disabled in my emulator, the new code worked as described and an error was thrown which I was able to catch and then handle appropriately.

I do still have an issue though. When I have Location Services enabled in my emulator, the code reaches const location = await FusedLocation.getFusedLocation();, but then FusedLocation.getFusedLocation() never returns anything, the code here simply hangs. So location never gets a value, and no error is thrown. As I mentioned above, I have a part of my app relying on getting a response back from the FusedLocation.getFusedLocation() call, so this is a problem for me.

My emulator is a Nexus 5 running API 23 (x86). I'm not exactly sure why FusedLocation.getFusedLocation() seems to hang, because Location Services is enabled on the phone, and in the emulator settings I have a GPS location set, and before this I have the code const granted = await PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION, {...}) which shows that this permission has been granted. But whatever the reason, FusedLocation does not currently handle this well and the call to FusedLocation.getFusedLocation() never returns anything.

@MustansirZia
Copy link
Owner

@jordanmkoncz Alright. If I may ask you a couple of things before we move ahead.
• Does it work the way it should when you use it on a physical device?
• Does it only happen inside the Nexus 5 emulator?
• Have you tried restarting the app AFTER you've turned ON location services and have set a GPS location?
• Does FusedLocation.startLocationUpdates() work? That is, are you able to receive latest location updates in the callback for FusedLocation.on('fusedLocation', callback) after you call FusedLocation.startLocationUpdates()?

@jordanmkoncz
Copy link

jordanmkoncz commented Oct 3, 2017

@MustansirZia sorry I haven't been able to get back to you on this, I've been working on another project that doesn't use this library. I probably won't get a chance to do more testing and answer all your questions until a few weeks from now. I'll try to give you some answers in the meantime.

Does it work the way it should when you use it on a physical device?

I did test it on a real device and the library worked correctly, but that was with Location Services enabled. I'm not sure exactly how I'd test for the error condition on a real device, I guess I'd have to turn off Location Services and also turn off something so that it can't get my location from Cell Towers either?

Does it only happen inside the Nexus 5 emulator?

I only have the Nexus 5 emulator set up right now so I wasn't able to test it in any other emulators.

Have you tried restarting the app AFTER you've turned ON location services and have set a GPS location?

I think I did try this, but I can't remember for sure.

Does FusedLocation.startLocationUpdates() work?

I didn't try using startLocationUpdates() when I was testing the new version.

I know those aren't the answers you were wanting, but hopefully that's helpful. :)

@jezravina91
Copy link

I'm experiencing something similar. On the emulator. If the locations is on at the start of the build. Everything works fine. But if you turn off then on the location services. It just keeps loading but not returning anything. I haven't tested this yet on an actual device. May just be a emulator problem.

Reference: facebook/react-native#16903

@MustansirZia
Copy link
Owner

MustansirZia commented Feb 10, 2018

Just thought I should comment here. Have added areProvidersAvailable method to the FusedLocation object. In here f5b56da.

Maybe call this method and check for a true in return before you initiate a location request? It checks if the GPS provider or the network provider is currently available on the device.

@jarvisluong
Copy link
Author

Hi! I think this issue is now resolved, should we close it now?

@MustansirZia
Copy link
Owner

Please do.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants