0

I face todays a problem with a fresh mobile. when i do :

mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(150000);
mLocationRequest.setFastestInterval(30000);
mLocationRequest.setMaxWaitTime(900000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
mLocationRequest.setSmallestDisplacement(25);

Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); 
if (location == null) { Log.w(TAG, "getLastLocation return null"); }
else { onLocationChanged(location); }

LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);

The problem is that LocationServices.FusedLocationApi.getLastLocation return null. normally this is not a big problem as we also setup requestLocationUpdates. BUT the problem is that requestLocationUpdates take a very long time to return. How can i say to requestLocationUpdates to return the fastest as possible the First location, and to return the following location at interval like we configured the mLocationRequest ?

zeus
  • 12,173
  • 9
  • 63
  • 184

1 Answers1

2

You should create 2 different location requests, the one you have for regular location updates, and also one like this for getting current location fast:

mLocationRequest2 = new LocationRequest();
mLocationRequest2.setInterval(500);
mLocationRequest2.setFastestInterval(0);
mLocationRequest2.setMaxWaitTime(1000);
mLocationRequest2.setNumUpdates(1);
mLocationRequest2.setPriority(LocationRequest.HIGH_ACCURACY);

The setNumUpdates will make sure this LocationRequest will be called only once.

HedeH
  • 2,869
  • 16
  • 25
  • so i can do at the same time 2 requests like LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); immediatly followed by LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest2, this); ? – zeus Apr 01 '18 at 15:13
  • Mmm.. I need to re-read the docs, but I don't think you can.. I think you need to switch between them when it suits your purpose. The separation is only for convenient. – HedeH Apr 01 '18 at 16:11