use the fused location provider to retrieve the device’s last known location. The fused location provider is one of the location APIs in Google Play services. It manages the underlying location technology and provides a simple API so that you can specify requirements at a high level, like high accuracy or low power. It also optimizes the device’s use of battery power.
To access the fused location provider, your app’s development project must include Google Play services. Download and install the Google Play services component via the SDK Manager and add the library to your project
Apps whose features use location services must request location permissions, depending on the use cases of those features.
To request the last known location, call the getLastLocation() method.
Example :
fusedLocationClient.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
}
}
});