iOS高德地图之定位

本文介绍了在iOS中使用高德地图进行定位的步骤,包括引入必要的头文件、配置Key以及设置期望的定位精度,特别强调了在后台模式下更新定位的重要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.0单次定位

第 1 步,引入头文件

引入AMapFoundationKit.h和AMapLocationKit.h这两个头文件:

1
2
3
#import <AMapFoundationKit/AMapFoundationKit.h>
 
#import <AMapLocationKit/AMapLocationKit.h>

第 2 步,配置Key

在调用定位时,需要添加Key,需要注意的是请在 SDK 任何类的初始化以及方法调用之前设置正确的 Key。

如果您使用的是定位SDK v2.x版本需要引入基础 SDK AMapLocationKit.framework ,设置apiKey的方式如下:

iOS 定位SDK v2.x版本设置 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);
    }
}
如果要后台定位:

依次执行:

a)左侧目录中选中工程名,开启 TARGETS->Capabilities->Background Modes

b)在 Background Modes中勾选 Location updates



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值