zeus
zeus

Reputation: 13367

FusedLocationApi.getLastLocation and requestLocationUpdates take very long time to return

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 ?

Upvotes: 0

Views: 446

Answers (1)

HedeH
HedeH

Reputation: 3034

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.

Upvotes: 2

Related Questions