Getting the last known location of the Android device

By Using the Google play services API, you can access the last known location of you device. Most of the time you are interested in user’s current location which is equivalent to device’s last known location.In this tutorial we will be using the fused location provider of the Google play services.

Setting Up Google Play Services


To access the fused location provider, your app’s project must have the google play services. You can download it via SDK Manager. For more information, check out the guide to Setting Up Google Play Services.

Specifying App Permissions


Apps using location services must request location permissions. Android allows two location permissions:  ACCESS_COARSE_LOCATION & ACCESS_FINE_LOCATION. The permission you choose determines the accuracy of the location returned by the API. If you specify ACCESS_COARSE_LOCATION, the API returns a location with an accuracy approximately equivalent to a city block whereas if your specify ACCESS_FINE_LOCATION, the accuracy of the location will be very precise.

In this tutorial we need only coarse location, so add the following code in the Manifest file of your app project.

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.techhiking.locationsample" >

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

Connecting to Google Play Services


To connect to the API, you need to create an instance of the Google Play services API client. In your Activity’s onCreate() method , write the following code-

// Create an instance of GoogleAPIClient.
if (mGoogleApiClient == null) {
    mGoogleApiClient = new GoogleApiClient.Builder(this)
        .addConnectionCallbacks(this)
        .addOnConnectionFailedListener(this)
        .addApi(LocationServices.API)
        .build();
}

Write the following code in your onStart() and onStop() respectively so as to connect and disconnect.

protected void onStart() {
    mGoogleApiClient.connect();
    super.onStart();
}

protected void onStop() {
    mGoogleApiClient.disconnect();
    super.onStop();
}

Getting the Last Known Location


Once you have connected to Google Play services and the location services API, you can access device location by calling the getLastLocation() method. Precision of the location returned will depend on the permission settings you entered in the manifest file.

To request the last known location, call the getLastLocation() method, passing it your instance of the GoogleApiClient object. Do this in the onConnected() callback provided by Google API Client, which is called when the client is ready. The following code snippet illustrates the request and a simple handling of the response:

public class MainActivity extends ActionBarActivity implements
        ConnectionCallbacks, OnConnectionFailedListener {
    ...
    @Override
    public void onConnected(Bundle connectionHint) {
        mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
                mGoogleApiClient);
        if (mLastLocation != null) {
            mLatitudeText.setText(String.valueOf(mLastLocation.getLatitude()));
            mLongitudeText.setText(String.valueOf(mLastLocation.getLongitude()));
        }
    }
}

The getLastLocation() returns an object of type Location and you can get the latitude and longitude by simply calling its getLatitude() and getLongitude() methods.

To know how to further get regular location updates and how to change device location settings from the app, check out Receiving regular Location Updates in Android.

Leave a Reply

Your email address will not be published. Required fields are marked *