Working with Fused Location Provider in Android

Fused location provider API is that you not need to worry about location updates it gives you always latest and accurate location and updates location after a interval or on location change .

it automatically choose best one suited for your hardware of your android device.
Utilizes low power by choosing the most efficient way to access the location

Three Use Cases for location access

With respect to fused location provider, we can broadly classify the API usage in three use cases.

  1. getLastLocation(GoogleApiClient) This API should be used when there is no need for continuous access to location from an application. Like one shot access or get user location based on some action.
    This is the simplified way to get the device location and also may not provide high accuracy.

  2. requestLocationUpdates(GoogleApiClient,LocationRequest, LocationListener) This API should be used when there a need for continuous location updates and the location is accessed when the application is active in foreground.

  3. requestLocationUpdates (GoogleApiClient, LocationRequest, PendingIntent) This API is used to receive location updates in the background even when the application is not active. So the difference is PendingIntent.

Prerequisite

Google Play Services is required. If you are using Android Studio and Gradle, you should have the following dependencies added in build.gradle file

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:21.0.3'
    compile 'com.google.android.gms:play-services:6.5.87'
}

Remember, You need not give permission for GPS, Network providers. I have seen Android location access tutorial examples giving all the available permission in the world.
Just on android.permission.ACCESS_FINE_LOCATION is enough.

FusedLocationProviderApi Usage

FusedLocationProviderApi

FusedLocationProviderApi can be accessed via LocationServices as below,

private FusedLocationProviderApi fusedLocationProviderApi = LocationServices.FusedLocationApi;

GoogleApiClient

FusedLocationProviderApi requires the GoogleApiClient instance to get the Location and it can be obtained as below.

googleApiClient = new GoogleApiClient.Builder(locationActivity)
            .addApi(LocationServices.API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();

Instantiating the GoogleApiClient should be done in onCreate of the application Activity.

googleApiClient.connect() should be done in onStart and disconnect() in onStop.

When the GoogleApiClient is connected successfully, onConnected(Bundle) callback will be called. There we should register with LocationListener for location updates as
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);

Location Interface Implementations for Callbacks

The following interfaces should be implemented to get the location update.

  • LocationListener provides call back for location change through onLocationChanged.
  • GoogleApiClient.ConnectionCallbacks provides call back for GoogleApiClient onConnected.
  • GoogleApiClient.OnConnectionFailedListener provides call back for GoogleApiClient onConnectionFailed.
Software Engineer(Android) at Bennett Coleman and Co. Ltd. (Times Group)

This content has been helpful to you?

Thanks for contributing!

Yes No