page.title=Getting the Last Known Location trainingnavtop=true @jd:body
Using the Google Play services location APIs, your app can request the last known location of the user's device. In most cases, you are interested in the user's current location, which is usually equivalent to the last known location of the device.
Specifically, use the fused location provider to retrieve the device's last known location. The fused location provider is one of the location APIs in Google Play services. It manages the underlying location technology and provides a simple API so that you can specify requirements at a high level, like high accuracy or low power. It also optimizes the device's use of battery power.
This lesson shows you how to make a single request for the location of a device using the {@code getLastLocation()} method in the fused location provider.
To access the fused location provider, your app's development project must include Google Play services. Download and install the Google Play services component via the SDK Manager and add the library to your project. For details, see the guide to Setting Up Google Play Services.
Apps that use location services must request location permissions. Android offers two location permissions: {@link android.Manifest.permission#ACCESS_COARSE_LOCATION ACCESS_COARSE_LOCATION} and {@link android.Manifest.permission#ACCESS_FINE_LOCATION ACCESS_FINE_LOCATION}. The permission you choose determines the accuracy of the location returned by the API. If you specify {@link android.Manifest.permission#ACCESS_COARSE_LOCATION ACCESS_COARSE_LOCATION}, the API returns a location with an accuracy approximately equivalent to a city block.
This lesson requires only coarse location. Request this permission with the {@code uses-permission} element in your app manifest, as the following code snippet shows:
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.google.android.gms.location.sample.basiclocationsample" > <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> </manifest>
To connect to the API, you need to create an instance of the Google Play services API client. For details about using the client, see the guide to Accessing Google APIs.
In your activity's {@link android.app.Activity#onCreate onCreate()} method, create an instance of Google API Client, using the {@code GoogleApiClient.Builder} class to add the {@code LocationServices} API, as the following code snippet shows.
// Create an instance of GoogleAPIClient. if (mGoogleApiClient == null) { mGoogleApiClient = new GoogleApiClient.Builder(this) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this) .addApi(LocationServices.API) .build(); }
To connect, call {@code connect()} from the activity's {@code onStart()} method. To disconnect, call {@code disconnect()} from the activity's {@code onStop()} method. The following snippet shows an example of how to use both of these methods.
protected void onStart() { mGoogleApiClient.connect(); super.onStart(); } protected void onStop() { mGoogleApiClient.disconnect(); super.onStop(); }
Once you have connected to Google Play services and the location services API, you can get the last known location of a user's device. When your app is connected to these you can use the fused location provider's {@code getLastLocation()} method to retrieve the device location. The precision of the location returned by this call is determined by the permission setting you put in your app manifest, as described in the Specify App Permissions section of this document.
To request the last known location, call the {@code getLastLocation()} method, passing it your instance of the {@code GoogleApiClient} object. Do this in the {@code 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 {@code getLastLocation()} method returns a {@code Location} object from which you can retrieve the latitude and longitude coordinates of a geographic location. The location object returned may be null in rare cases when the location is not available.
The next lesson, Changing Location Settings, shows you how to detect the current location settings, and prompt the user to change settings as appropriate for your app's requirements.