8

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?

Zishan Neno
  • 2,647
  • 7
  • 34
  • 58

2 Answers2

2

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

wscourge
  • 10,657
  • 14
  • 59
  • 80
1

Gps does not always return a location even it's available through FusedLocationProvider api or LocationManager LocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER). Especially, if you are indoors it's less likely to get location from Gps depending on your device's Gps chip. Gps must be fixed to return a location.

You can write a test app which has LocationManager with Gps, Wifi and GpsStatusListener to check satellite count, satellites in the fix, and satellite signal strength and compare it with FusedLocationProvider at the same time and add the locations provided to map as markers. You can also look for Location.getExtras() for FLP which may contain info about satellites. Even if LocationManager Gps is available and satellite count is 2 i can not get a location from Gps.

Hello World in image should be Gps location but it's not returned since it's not fixed yet. LocationManager Gps Status test Testing may not solve your problem but you can compare situations you get location or you can not for example it may required to use 3 satellites to get location from Gps or minimum signal strength. It may also take too long for Gps to lock and status on GpsStatusListener is set to GpsStatus.GPS_EVENT_FIRST_FIX when it's locked. It took about 5 minutes for me Gps to get fixed and return a location. I also wrote another test with maps and i had the same issue until Gps is fixed on both LocationManager and FLP.

Thracian
  • 43,021
  • 16
  • 133
  • 222