I made changes in my location based android app to conform to latest API. So now my activity implements:
public class MapActivity extends FragmentActivity implements LocationListener, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
.....
}
In onCreate
, I configure LocationRequest
and GoogleApiClient
:
mLocationRequest = LocationRequest.create()
.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY)
.setInterval(2 * 1000);
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
And here is my onConnected
method:
@Override
public void onConnected(Bundle bundle) {
Log.i(TAG, "Location services connected.");
Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if (location == null) { LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
} else {
.....
.....
}
}
Issue I am facing is that onLocationChanged
is not getting called even when user is on move.
What am I missing here?
During debugging I found that in onConnected()
:
Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
The location is not null, hence code inside if
is not executed. Can that be the reason for onLocationChanged
not getting called?