Google’s Fused Location API for Android

17 / Jul / 2014 by Himanshu Singh 9 comments

For android applications, the Fused Location Provider intelligently manages the underlying location technology and gives us the best location according to our needs.

We could choose one of the location providers (network or GPS) and request location updates or set up proximity alert. But there were two main challenges with this approach:

1. In case we need to define precise location, we had to switch between network and GPS location providers (as GPS doesn’t work indoors)
2. Proximity alerts were used to notify a user about proximity to a location, and this took its toll on the battery life

Advantages of Using API

1. Simple APIs: Lets us specify high-level needs like “high accuracy” or “low power”, instead of having to worry about location providers.
2. Immediately Available: Gives our apps immediate access to the best, most recent location
3. Power-Efficiency: Minimizes power usage by the apps. Based on all incoming location requests and available sensors, fused location provider picks the most efficient way to meet those needs.
4. Versatility: Meets a wide range of needs, from foreground uses that need highly accurate location to background uses that need periodic location updates with negligible power impact.

Note : Make sure Google Play services is properly installed and working in our device. Please don’t test this location API in an emulator as this does not work.

Ways to Use

1. Import Google Play services from Android SDK (../sdk/extras/google/google_play_service/libproject/google-play-services_lib) as a library in our application

2.
Declare the permission in the manifest file.

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

Android has two location permissions, ACCESS_COARSE_LOCATION and ACCESS_FINE_LOCATION.

The permissions that we choose affects the accuracy of the location updates we receive. For example, if we request only coarse location permission, Location Services obfuscates the updated location to an accuracy that is roughly equivalent to a city block.

Requesting ACCESS_FINE_LOCATION implies a request for ACCESS_COARSE_LOCATION.

3. There is two main class which are most important in this server
3.1 com.google.android.gms.location.LocationClient : Stores the current instantiation of the location client
3.2 com.google.android.gms.location.LocationRequest : A data object that contains quality of service parameters for requests to the LocationClient

4. Location Service Callbacks : Before we request location updates, we must first implement the interfaces that Location Services uses to communicate connection status to our app:
4.1com.google.android.gms.common.GooglePlayServicesClient.ConnectionCallbacks: Specifies methods that Location Services calls when a location client is connected or disconnected


new  GooglePlayServicesClient.ConnectionCallbacks() {

			@Override
			public void onDisconnected() {
				// TODO Auto-generated method stub

			}

			@Override
			public void onConnected(Bundle arg0) {
				// TODO Auto-generated method stub

			}

		};

4.2com.google.android.gms.common.GooglePlayServicesClient.OnConnectionFailedListener : Specifies a method that Location Services calls if an error occurs while attempting to connect the location client.


new GooglePlayServicesClient.OnConnectionFailedListener() {
	@Override
	public void onConnectionFailed(ConnectionResult arg0) {
	}
};

How to Use

1. Connecting LocationClient to Google API. After connecting locationClient, we are able to get location
2. Retrieving the current location
3. Retrieving the location update at particular time interval or particular distance or both.

1. Connecting LocationClient to Google API:

// Create a new global location parameters object
LocationRequest mLocationRequest = LocationRequest.create();

//Set the update interval
mLocationRequest.setInterval(LocationUtils.UPDATE_INTERVAL_IN_MILLISECONDS);

// Use high accuracy
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

/**
* LocationClient(arg1, arg2 , arg3)
* arg1 :Context
* arg2 : ConnectionCallbacks
* arg3 :OnConnectionFailedListener
**/

LocationClient  mLocationClient = new LocationClient(arg1, arg2 , arg3);
mLocationClient.connect();

/**
* Note: connect() method will take some time. So for getting the
* current location or getting location update at particular inteval,
* we have to wait for the connectin established.
**/

During the Google IO presentation, a chart was presented showing effect of different priorities of the recognition algorithm as tested multiple times on Galaxy Nexus

Priority Typical location
update interval
Battery drain
per hour (%)
Accuracy
HIGH_ACCURACY 5 seconds 7.25% ~10 meters
BALANCED_POWER 20 seconds 0.6% ~40 meters
NO_POWER N/A small ~1 mile


2. Retrieving The Current Location :

Location myLocation = mLocationClient.getLastLocation();
/**
* Now MyLocation object has all infromation
* like  latitude,longitude etc.
**/

The return value is the best, most recent location, based on the permissions our app requested and the currently-enabled location sensors.

Before we create the location client, implement the interfaces that Location Services uses to communicate with our app:

Reference : https://developer.android.com/training/location/retrieve-current.html

3. Retrieving Location Updates : To get periodic location updates from location services, we send a request using a location client. Depending on the form of the request, location services either invokes a callback method and passes in a location object, or issues an intent that contains the location in its extended data. The accuracy and frequency of the updates are affected by the location permissions we’ve requested and the parameters we pass to location services with the request.

LocationListener listener =  new LocationListener() {

			@Override
			public void onLocationChanged(Location location) {
	  	// Report to the UI that the location was updated
        String msg = "Updated Location: " +
                Double.toString(location.getLatitude()) + "," +
                Double.toString(location.getLongitude());
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();

			}
		};

mLocationClient.requestLocatonUpdates(mLocationRequest,listener);

Reference : https://developer.android.com/training/location/receive-location-updates.html

You might also like reading our blog on Background Location Updates on Android

FOUND THIS USEFUL? SHARE IT

comments (9)

    1. surya prakash sahu

      Emulator dont have the gps tracker.To work you have to use it in real device,i.e mobile phones.

      Reply
    2. surya prakash sahu

      How to get update with specific time and distance??
      You did’nt used anything in your code for that.
      please reply as soon as possible

      Reply

Leave a comment -