When I check Android tutorials and/or the Android official documentation, it seems multiple different ways are there to query location. I am confused as I am not sure which way is the right way or if documentation is outdated.
For example,
1) GoogleApiClient: In this way, it uses the Google API client
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
and then it queries locations like this
LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
2) Location Manager: This way uses location Manager
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
Location lastKnownLocation = locationManager.getLastKnownLocation(locationProvider);
3) FusedLocationApi (2nd style):
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
mFusedLocationClient.getLastLocation()
.addOnSuccessListener(this, new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
// Got last known location. In some rare situations, this can be null.
if (location != null) {
// Logic to handle location object
}
}
});
Which way should we use?