Using the Google Play services location APIs, your app can request the last known location of the user’s device.
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.
To protect user privacy, apps that use location services must request location permissions.
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);
}
The FusedLocationProviderClient
provides many ways to get the device location data,as the following:
getLastLocation() :it get the location quickly and minimize the battery usage ,but the location might be out of date if we don’t use this information fast.
getCurrentLocation() :it get updated and give accurate location but it still do computation on the device. this way is the best to get a fresh location ,and safer than using requestLocationUpdates()
.