Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fused Location GPS doesn't update until opening Google Maps

I'm using fused location provider in my app which is set to PRIORITY_HIGH_ACCURACY at an interval of 60 seconds. This usually works but sometimes the location doesn't update for 5 to 10 minutes by itself, and I when I open Google Maps, it seems to trigger a location update.

Anyone else here who might have run into this issue or knows of the possible causes behind this behavior? Is there any way to fix this?

like image 932
Zishan Neno Avatar asked Jan 17 '17 18:01

Zishan Neno


1 Answers

My findings from the following link the Fused Location Provider will only maintain background location if at least one client is connected to it. Once the first client connects, it will immediately try to get a location. If your activity is the first client to connect and you call getLastLocation() right away in onConnected(), that might not be enough time for the first location to come in. This will result in the location being null.

To solve this issue, you have to wait (indeterminately) till the provider gets the location and then call getLastLocation(), which is impossible to know.

Another (better) option is to implement the com.google.android.gms.location.LocationListener interface to receive periodic location updates (and switch it off once you get the first update).

like image 151
Ikram Hussain Avatar answered Oct 05 '22 10:10

Ikram Hussain