This is the code that gets called when the GoogleApiClient gets created. All this does it retrieve the last location of the user.
//omitted code
@Override
public void onConnected(Bundle bundle) {
Location location = LocationServices.FusedLocationApi.getLastLocation(mApiClient);
if (location != null) {
mLongitude = String.valueOf(location.getLongitude());
mLatitude = String.valueOf(location.getLatitude());
Log.d(TAG, mLongitude + "___" + mLatitude); //this one doesn't return null
} else {
Log.d(TAG, "Location is null");
LocationServices.FusedLocationApi.requestLocationUpdates(mApiClient, mLocationRequest, this);
}
}
This is for the google api client:
private void buildGoogleApiClient() {
mApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
This snippet is from my onCreate() method:
buildGoogleApiClient();
Log.d(TAG, mLongitude + "___" + mLatitude); //this one returns null
So when the code gets run, i build a google api client and the onConnected method gets called. Inside of that method, i will update the mLongitude and mLatitude variables which are instance variables. However in the log, they return as null. I was wondering why this happened so i put another log INSIDE the if statement of my onConnected() method as you can see in the first block of code and this one successfully brings back the longitude and latitude. I dont know why its not updating the instance variables. I believe it's because the callback isn't being run on the main thread and so the log happens before the update. So I tried running the code in the main thread like this:
runOnUiThread(new Runnable() {
@Override
public void run() {
buildGoogleApiClient();
}
});
Log.d(TAG, mLongitude + "___" + mLatitude);
unfortunately this doesnt work either. I searched for things relating to this topic and tried implementing them in my code however none seem to work as i dont even know the real cause for this problem.
Anyone know how to fix this?
Thank you