The NEW Android Location API

Android | Tuesday September 2 2014 11:18 | Comments (3)

Since the release of the new Google Play Services 4.2, some new APIs have been added and Google have changed their Client API Model. One such API model to be affected is the Location API.

If you are targeting the latest version of Android in your app, you will see that your existing Location API code will now be flagged as being deprecated.

Previously, you would interact with the Location API as follows:

    LocationManager locationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    String provider = locationManager.getBestProvider(criteria, false);
    locationManager.requestLocationUpdates(provider, updateInterval, 0, this);

    public void onLocationChanged(Location location) {
        //Handle location update

Now with the new Location API, it will look similar to this:

    GoogleApiClient mGoogleApiClient;
    LocationRequest mLocationRequest;
    mGoogleApiClient = new GoogleApiClient.Builder(this)
    .addConnectionCallbacks((GoogleApiClient.ConnectionCallbacks) this)

    public void onConnected(Bundle arg0) {
        mLocationRequest = LocationRequest.create();
        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);

    public void onLocationChanged(Location location) {
        //Handle location update

You’ll need to import: (thanks Mark);;
(note it’s the GoogleApiClient, instead of the GooglePlayServicesClient version).

As well as LocationListener, your class also needs to implement ConnectionCallbacks and OnConectionFailedListener (assuming the same class is handling all the callbacks).

Also, onDisconnected() has been renamed to onConnectionSuspended(int cause).


  1. Comment by Mark — 10th January 2015 @ 02:06

    The line below doesn’t work:
    LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);

    Android Studio tries to cast ‘this’ to:( this
    Which also doesn’t work!!

    java.lang.ClassCastException: MapsActivity cannot be cast to

    Any help would be appreciated 🙂

  2. Comment by Mark — 10th January 2015 @ 05:34

    LOL, Found the issue, wrong location import was used. There are two, an android one and a google play one. Use the google play one (below):
    delete the import (1) android.location.Location;
    add the import (2) import;

  3. Comment by Jeremy — 2nd July 2015 @ 05:55

    Thank you, @Mark. I had this same issue.

RSS feed. TrackBack URI

Leave a comment