View on GitHub

reads

https://yazansneneh.github.io/reads/

Location

Get the last known location

Set up Google Play services

Create location services client
  1. In activity’s onCreate() method.
  2. create an instance of the Fused Location Provider Client.
private FusedLocationProviderClient fusedLocationClient;

// ..

@Override
protected void onCreate(Bundle savedInstanceState) {
    // ...

    fusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
}
  1. Get the last known location
    • To request the last known location, call the getLastLocation() method.
    • returns a Task that I use to get a Location object with the latitude and longitude coordinates of a geographic location.
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
                }
            }
        });