Working with Fused Location Provider in Android

Fused location provider API is a tool that allows the developer not to worry about location updates. It gives you the latest and most accurate location of the user and updates the location when the user changes his position or after certain intervals.
It automatically chooses the better of the two above mentioned options based on the hardware of user’s android device.
It 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 is used when there is no need for continuous access to user’s location. This is similar to one shot access or getting user location based on some action. This is the simplified way to get the device location but may not provide high accuracy.

  2. requestLocationUpdates(GoogleApiClient,LocationRequest, LocationListener) This API is used when there is 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 for Fused location provider API. 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 do not need to give permission for GPS, Network providers. Just android.permission.ACCESS_FINE_LOCATION is enough.

FusedLocationProviderApi Usage

FusedLocationProviderApi

FusedLocationProviderApi can be accessed via LocationServices as shown below,

private FusedLocationProviderApi fusedLocationProviderApi = LocationServices.FusedLocationApi;

GoogleApiClient

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

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

Instantiating the GoogleApiClient is done in onCreate function of the application Activity.

googleApiClient.connect() should be called in the onStart function and disconnect() in the onStop function.

When the GoogleApiClient is connected successfully, onConnected(Bundle) callback is 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 are 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