Document Location SDK for Android Get Started

Get Started

This guide walks you through the steps required to add location functionality to your Android app using the AMAP Location SDK.

Step 1: Download and Install Android Studio

Download and install Android Studio by following the official setup guides:

Download Android Studio

Installation Guide

Note: The download links above point to Google's official website.

Step 2: Obtain an AMAP Key

To use the AMAP SDK, you must first obtain an API key from the AMAPConsole.

If this is your first time using the service, follow the Get a Key guide to register and generate your AMAP key.

Step 3: Create a Project

Create a new Android project with an Empty Activity template by following these steps:

1. Launch Android Studio. If the Welcome to Android Studio dialog appears, click Start a new Android Studio project. Otherwise, from the menu bar, select File > New > New Project. Enter your application name, company domain, and project location, then click Next.

2. Select the form factors your app will run on. If you are unsure, choose Phone and Tablet, then click Next.

3. In the Add an activity to Mobile dialog, select Empty Activity, then click Next.

4. Enter the Activity name, layout name, and title as prompted. The default values are acceptable. Click Finish.

Step 4: Download and Install the Location SDK

Download the location SDK package from the official download page. Extract the downloaded archive.

After extraction, you will find a JAR file. Add this JAR file to your project. For detailed instructions, refer to the JAR file installation guide for Android Studio.

Step 5: Hello AMAPLocation

Review and configure the following files in your Android Studio project.

5.1 Configure AndroidManifest.xml

By default, the AndroidManifest.xml file is located under the main directory of your project. This file is used to configure your AMAP key and declare required permissions.

Configure the AMAP key inside the <application> tag:

<meta-data
    android:name="com.amap.api.v2.apikey"
    android:value="Your AMAP Key" />

Declare the location service component inside the <application> tag:

<service android:name="com.amap.api.location.APSService" />

Declare required permissions in AndroidManifest.xml:

<!-- Required: Allow network access -->
<uses-permission android:name="android.permission.INTERNET" />

<!-- Required for precise location -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<!-- Required for approximate location -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

<!-- Required for network-based positioning (when GPS is unavailable) -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<!-- Required for network-based positioning (when GPS is unavailable) -->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

<!-- Required for network-based positioning (when GPS is unavailable) -->
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />

<!-- Required for background location access -->
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />

<!-- Required for A-GPS module acceleration -->
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />

<!-- Optional: Write device cache for troubleshooting -->
<uses-permission android:name="android.permission.WRITE_SETTINGS" />

5.2 Implement Location

The following code demonstrates how to implement basic location functionality:

// Declare the AMapLocationClient object
public AMapLocationClient mLocationClient = null;

// Initialize the location client
mLocationClient = new AMapLocationClient(getApplicationContext());

// Set the location callback listener
mLocationClient.setLocationListener(mLocationListener);

// Start location updates
mLocationClient.startLocation();

// Asynchronously receive location results
AMapLocationListener mAMapLocationListener = new AMapLocationListener() {
    @Override
    public void onLocationChanged(AMapLocation amapLocation) {
        if (amapLocation != null) {
            if (amapLocation.getErrorCode() == 0) {
                // Parse the location result
            }
        }
    }
};

Step 6: Connect an Android Device

The simplest way to test your app is to connect an Android device to your computer. Enable Developer options on your device and configure your system to detect it.

Alternatively, you can use the Android Emulator. Use the Android Virtual Device (AVD) Manager to create and configure one or more virtual devices, then run your app on the emulator.

Step 7: Build and Run Your App

In Android Studio, click the Run menu option (or the Play button icon) to build and run your app.

When prompted to select a deployment target, choose one of the following:

- Select the Android device connected to your computer.

- Select the Launch emulator radio button, then choose a previously configured virtual device.

Click OK. Android Studio uses Gradle to build your app and displays the result on the selected device or emulator. The app may take a few minutes to launch.