1.0单次定位
第 1 步,引入头文件
1
2
3
|
#import <AMapFoundationKit/AMapFoundationKit.h>
#import <AMapLocationKit/AMapLocationKit.h>
|
第 2 步,配置Key
1
|
[AMapServices sharedServices].apiKey =@
"您的key"
;
|
第 3 步,设置期望定位精度
self.locationManager = [[AMapLocationManager alloc] init];
self.locationManager.delegate = self;
[self.locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
[self.locationManager setPausesLocationUpdatesAutomatically:NO];
[self.locationManager setAllowsBackgroundLocationUpdates:YES];
//带逆地理定位
[self.locationManager setLocatingWithReGeocode:YES];
[self.locationManager startUpdatingLocation];
#pragma mark --AMapLocationManagerDelegate
- (void)amapLocationManager:(AMapLocationManager *)manager didFailWithError:(NSError *)error
{
//定位错误
NSLog(@"%s, amapLocationManager = %@, error = %@", __func__, [manager class], error);
}
- (void)amapLocationManager:(AMapLocationManager *)manager didUpdateLocation:(CLLocation *)location
{
//定位结果
// NSLog(@"location:{lat:%f; lon:%f; accuracy:%f}", location.coordinate.latitude, location.coordinate.longitude, location.horizontalAccuracy);
}
//可在此添加大头针,更新地图。
- (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 (!self.annotation) {
self.annotation = [[MAPointAnnotation alloc] init];
}
self.annotation.coordinate = location.coordinate;
[self.mapView addAnnotation:self.annotation];
self.annotation.title = reGeocode.city;
self.annotation.subtitle = reGeocode.AOIName;
self.showLabel.text = reGeocode.AOIName;
[self.mapView setCenterCoordinate:location.coordinate];
[self.mapView setZoomLevel:15.1 animated:NO];
if (reGeocode)
{
NSLog(@"reGeocode:%@", reGeocode);
}
}
如果要后台定位: