Location Data

One of the unique features of mobile applications is location awareness. Mobile users bring their devices with them everywhere, and adding location awareness to your app offers users a more contextual experience.

Code samples

The ApiDemos repository on GitHub includes samples that demonstrate the use of location on a map:

Working with location data

The location data available to an Android device includes the current location of the device — pinpointed using a combination of technologies — the direction and method of movement, and whether the device has moved across a predefined geographical boundary, or geofence. Depending upon the needs of your application, you can choose between several ways of working with location data:

  • The My Location layer provides a simple way to display a device's location on the map. It does not provide data.
  • The Google Play services Location API is recommended for all programmatic requests for location data.
  • The LocationSource interface allows you to provide a custom location provider.

Location permissions

If your app needs to access the user's location, you must request permission by adding the relevant Android location permission to your app.

Android offers two location permissions: ACCESS_COARSE_LOCATION and ACCESS_FINE_LOCATION. The permission you choose determines the accuracy of the location returned by the API. You only need to request one of the Android location permissions, depending on the level of accuracy you need:

  • android.permission.ACCESS_COARSE_LOCATION – Allows the API to use WiFi or mobile cell data (or both) to determine the device's location. The API returns the location with an accuracy approximately equivalent to a city block.
  • android.permission.ACCESS_FINE_LOCATION – Allows the API to determine as precise a location as possible from the available location providers, including the Global Positioning System (GPS) as well as WiFi and mobile cell data.

Add the permissions to the app manifest

Add one of the following permissions as a child of the <manifest> element in your Android manifest. Either the coarse location permission:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myapp" >
  <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

Or the fine location permission:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myapp" >
  <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

Request runtime permissions

Android 6.0 (Marshmallow) introduces a new model for handling permissions, which streamlines the process for users when they install and upgrade apps. If your app targets API level 23 or later, you can use the new permissions model.

If your app supports the new permissions model and the device is running Android 6.0 (Marshmallow) or later, the user does not have to grant any permissions when they install or upgrade the app. The app must check to see if it has the necessary permission at runtime, and request the permission if it does not have it. The system displays a dialog to the user asking for the permission.

For best user experience, it's important to request the permission in context. If location is essential to the functioning of your app, then you should request the location permission at app startup. A good way to do this is with a warm welcome screen or wizard that educates users about why the permission is required.

If the app requires the permission for only part of its functionality, then you should request the location permission at the time when the app performs the action that requires the permission.

The app must gracefully handle the case where the user does not grant permission. For example, if the permission is needed for a specific feature, the app can disable that feature. If the permission is essential for the app to function, the app can disable all its functionality and inform the user that they need to grant the permission.

The following code sample checks for permission using the Support library before enabling the My Location layer:

if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
        == PackageManager.PERMISSION_GRANTED) {
    if (mMap != null) {
} else {
    // Permission to access the location is missing. Show rationale and request permission
    PermissionUtils.requestPermission(this, LOCATION_PERMISSION_REQUEST_CODE,
        Manifest.permission.ACCESS_FINE_LOCATION, true);

The following sample handles the result of the permission request by implementing the ActivityCompat.OnRequestPermissionsResultCallback from the Support library:

public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {

    if (PermissionUtils.isPermissionGranted(permissions, grantResults, Manifest.permission.ACCESS_FINE_LOCATION)) {
        // Enable the my location layer if the permission has been granted.
    } else {
        // Permission was denied. Display an error message
        // ...

For more code samples and best practices for Android runtime permissions, see the documentation for the Android permissions model.

The My Location layer

You can use the My Location layer and the My Location button to show your user their current position on the map. Call mMap.setMyLocationEnabled() to enable the My Location layer on the map.

The following sample shows a simple usage of the My Location layer:

public class MyLocationDemoActivity extends FragmentActivity
    implements OnMyLocationButtonClickListener,
        OnMapReadyCallback {

  private GoogleMap mMap;

  protected void onCreate(Bundle savedInstanceState) {

    SupportMapFragment mapFragment =
        (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);

  public void onMapReady(GoogleMap map) {
    mMap = map;
    // TODO: Before enabling the My Location layer, you must request
    // location permission from the user. This sample does not include
    // a request for location permission.

  public void onMyLocationClick(@NonNull Location location) {
    Toast.makeText(this, "Current location:\n" + location, Toast.LENGTH_LONG).show();

  public boolean onMyLocationButtonClick() {
    Toast.makeText(this, "MyLocation button clicked", Toast.LENGTH_SHORT).show();
    // Return false so that we don't consume the event and the default behavior still occurs
    // (the camera animates to the user's current position).
    return false;

When the My Location layer is enabled, the My Location button appears in the top right corner of the map. When a user clicks the button, the camera centers the map on the current location of the device, if it is known. The location is indicated on the map by a small blue dot if the device is stationary, or as a chevron if the device is moving.

The following screenshot shows the My Location button at top right and the My Location blue dot in the center of the map:

You can prevent the My Location button from appearing by calling UiSettings.setMyLocationButtonEnabled(false).

Your app can respond to the following events:

From our Terms of Service

Protect user privacy,
keep them informed

Always inform users of how you will use their data, and don't make it possible to identify individual users. Get user consent before using their location, and let them revoke consent at any time.

Learn More

The Google Play services Location API

The Google Play services Location API is the preferred method for adding location awareness to your Android application. It includes functionality that lets you:

  • Determine the device location.
  • Listen for location changes.
  • Determine the mode of transportation, if the device is moving.
  • Create and monitor predefined geographical regions, known as geofences.

The location APIs make it easy for you to build power efficient, location-aware applications. Like the Maps SDK for Android, the Location API is distributed as part of the Google Play services SDK. For more information on the Location API, please refer to the Android training class Making Your App Location Aware or the Location API Reference. Code examples are included as part of the Google Play services SDK.