Continuous Location
The iOS Location SDK provides continuous location functionality to obtain location data (which can be displayed using the Map SDK). This feature works similarly to CLLocationManager.
Before You Begin
Step 1: Import the Headers
Import the AMapFoundationKit.h and AMapLocationKit.h headers in the class where you call the location feature. For Swift, import these headers in your bridging header file.
#import <AMapFoundationKit/AMapFoundationKit.h>
#import <AMapLocationKit/AMapLocationKit.h>Step 2: Configure the API Key
Set your API key before initializing any SDK class or calling any SDK method.
For Location SDK v2.x:
[AMapServices sharedServices].apiKey = @"Your API Key";> Note: If you are using Location SDK v1.x, please update to v2.x as soon as possible.
For Location SDK v1.x:
[AMapLocationServices sharedServices].apiKey = @"Your API Key";Step 3: Initialize the Location Manager
Initialize an AMapLocationManager object and set its delegate.
- (void)viewDidLoad {
self.locationManager = [[AMapLocationManager alloc] init];
self.locationManager.delegate = self;
}Set the minimum distance filter (in meters):
When the distance between two consecutive location updates meets the specified minimum distance, the SDK returns the location result.
self.locationManager.distanceFilter = 200;Enable address description for continuous location updates:
> Note: Address descriptions are only available in China. They are not returned in overseas regions.
// Enable address description
self.locationManager.locatingWithReGeocode = YES;Step 4: Start Continuous Location
Call the startUpdatingLocation method on the AMapLocationManager instance.
[self.locationManager startUpdatingLocation];To receive reverse geocoding information with continuous location updates (supported since v2.2.0):
[self.locationManager setLocatingWithReGeocode:YES];
[self.locationManager startUpdatingLocation];Step 5: Handle Location Updates
Implement the amapLocationManager:didUpdateLocation: delegate method from AMapLocationManagerDelegate to process location updates.
- (void)amapLocationManager:(AMapLocationManager *)manager didUpdateLocation:(CLLocation *)location
{
NSLog(@"location:{lat:%f; lon:%f; accuracy:%f}", location.coordinate.latitude, location.coordinate.longitude, location.horizontalAccuracy);
}Using the reverse geocoding callback (since v2.2.0):
The amapLocationManager:didUpdateLocation:reGeocode: method returns both the location and reverse geocoding information in a single callback. Note that if you implement this method, the amapLocationManager:didUpdateLocation: method will no longer be called.
- (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 6: Stop Continuous Location
When location updates are no longer needed, call the stopUpdatingLocation method.
[self.locationManager stopUpdatingLocation];