Using the Google Play services location APIs, your app can request the last known location of the user’s device. In most cases, you are interested in the user’s current location, which is usually equivalent to the last known location of the device. Specifically, 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. For details, see the guide to Setting Up Google Play Services.
Apps whose features use location services must request location permissions, depending on the use cases of those features.
In your activity’s onCreate() method, create an instance of the Fused Location Provider Client as the following code snippet shows. private FusedLocationProviderClient fusedLocationClient;
// ..
@Override protected void onCreate(Bundle savedInstanceState) { // …
fusedLocationClient = LocationServices.getFusedLocationProviderClient(this); }
Once you have created the Location Services client you can get the last known location of a user’s device. When your app is connected to these you can use the fused location provider’s getLastLocation() method to retrieve the device location. The precision of the location returned by this call is determined by the permission setting you put in your app manifest, as described in the guide on how to request location permissions. The getLastLocation() method returns a Task that you can use to get a Location object with the latitude and longitude coordinates of a geographic location. The location object may be null in the following situations: