You're all set!

To start developing, please head over to our developer documentation.

Activate the Google Maps Android API

To get you started we'll guide you through the Google Developers Console to do a few things first:

  1. Create or choose a project
  2. Activate the Google Maps Android API
  3. Create appropriate keys
Continue

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"/>
  ...
</manifest>

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"/>
  ...
</manifest>

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) {
    mMap.setMyLocationEnabled(true);
} else {
    // Show rationale and request permission.
}

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

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    if (requestCode == MY_LOCATION_REQUEST_CODE) {
      if (permissions.length == 1 &&
          permissions[0] == Manifest.permission.ACCESS_FINE_LOCATION &&
          grantResults[0] == PackageManager.PERMISSION_GRANTED) {
        mMap.setMyLocationEnabled(true);
    } 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 provide your user with their current position on the map.

Enable the My Location layer on the map as follows:

mMap.setMyLocationEnabled(true);

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.

My Location button

A click also triggers the GoogleMap.OnMyLocationButtonClickListener.

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

Note that the My Location layer does not return any data. If you wish to access location data programmatically, use the Location API.

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 Google Maps Android API, 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.

Send feedback about...

Google Maps Android API
Google Maps Android API
Need help? Visit our support page.