Get Started
This guide walks you through the steps required to start using the AMAP Location SDK in your iOS project.
Step 1: Get the Latest Version of Xcode
We recommend using Xcode 8.0 or later to develop projects with the AMAP iOS SDK. Download Xcode from the Apple Developer website.
Step 2: Install CocoaPods
The AMAP iOS SDK is distributed as a CocoaPods pod. CocoaPods is an open-source dependency manager for Swift and Objective-C Cocoa projects.
If you haven't installed CocoaPods yet, run the following command in a terminal on macOS:
$ sudo gem install cocoapodsFor more information, see the CocoaPods Getting Started guide.
Step 3: Install the SDK Using CocoaPods
Create a Podfile for your project and use it to install the AMAP iOS SDK.
1. Open a terminal in your project directory (where your .xcodeproj file is located) and create a file named Podfile. If you haven't created an Xcode project yet, create one now and save it to your local machine. If this is your first iOS project, create a Single View Application.
$ touch Podfile2. Edit the Podfile and save it with the following content:
target "YourProjectTarget" do
pod 'AMapLocation'
endNote: The pod 'AMapLocation' command also installs the Foundation SDK. For information about how this affects App Store submission, see the Foundation SDK introduction.
3. Run the pod install command to install the SDK:
$ pod installIf the latest version of the SDK cannot be installed, run pod repo update to update your local CocoaPods repository.
4. Close Xcode, then open (double-click) your project's .xcworkspace file to launch Xcode. From this point forward, you must use the .xcworkspace file to open your project.
Step 4: Get an AMAP Key
Go to the AMAP Platform Console to apply for an iOS Key.
Step 5: Request Permissions
Location Permission (Required)
Use case: Obtain the user's real-time location to determine their actual position.
Configuration: For details on configuring location permissions, see iOS Location SDK Permission Configuration.
Before using the Location SDK, you must import the Foundation SDK. For configuration details, see the Foundation SDK introduction.
Step 6: Hello, AMAPLocation!
1. Configure the Info.plist File
Because the app requests location permissions, add the NSLocationAlwaysUsageDescription key to your Info.plist file.
2. Configure the AMAP Key in the AppDelegate.m File
Add the AMAP Key you obtained in Step 4 to your code to enable location functionality.
#import <AMapFoundationKit/AMapFoundationKit.h>
// Import the header file
……
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
……
[AMapServices sharedServices].apiKey = @"Your Key";
……
}3. Implement Continuous Location Updates
#import <AMapLocationKit/AMapLocationKit.h>
// Initialize the AMapLocationManager object and set the delegate.
- (void)viewDidLoad
{
self.locationManager = [[AMapLocationManager alloc] init];
self.locationManager.delegate = self;
}
// Call the startUpdatingLocation method to begin continuous location updates.
[self.locationManager startUpdatingLocation];
// Implement the delegate method to handle location updates.
- (void)amapLocationManager:(AMapLocationManager *)manager didUpdateLocation:(CLLocation *)location reGeocode:(AMapLocationReGeocode *)reGeocode
{
NSLog(@"location:{lat:%f; lon:%f; accuracy:%f}", location.coordinate.latitude, location.coordinate.longitude, location.horizontalAccuracy);
if (regeocode)
{
NSLog(@"reGeocode:%@", regeocode);
}
}Step 7: Connect an iOS Device
The simplest way to test your app is to connect an iOS device to your computer. On the device, trust your developer certificate and enable location services.
Alternatively, you can use the iOS Simulator included with Xcode. Select a simulator to build and run your app.
Step 8: Build and Run Your App
In Xcode, click Product > Run, or click the Run button, to build and run your app.