1.高德开放平台
在高德开放平台申请账号进行登录,进入控制台创建新应用



为应用添加Key



2.xcode配置
先从高德开放平台下载基础SDK和定位SDK,导入工程当中;然后添加系统依赖库:libc++.tbd,libstdc++.6.0.9.tbd,libz.tbd,SystemConfiguration.framework, CoreTeleohony.framework


在info.plist中添加网络安全设置和申请定位权限,NSLocationWhenInUseUsageDescription 表示应用在前台的时候可以搜到更新的位置信息,NSLocationAlwaysUsageDescription 表示应用在前台和后台(suspend 或 terminated)都可以获取到更新的位置数据。

3.代码实现
在app delegate.m中设置上面创建应用后添加的Key

在要进行定位的地方添加以下代码
//导入基础库和定位库
#import<AMapLocationKit/AMapLocationKit.h>
#import<AMapFoundationKit/AMapFoundationKit.h>
@property (nonatomic, strong)AMapLocationManager * locationManager; //位置管理类
//创建管理器对象
self.locationManager = [[AMapLocationManager alloc] init];
//设置精度
[self.locationManager setDesiredAccuracy:kCLLocationAccuracyHundredMeters];
//设置定位超时时间
self.locationManager.locationTimeout = 10;
//设置反地理编码超时时间
self.locationManager.reGeocodeTimeout = 10;
//请求定位
[self.locationManager requestLocationWithReGeocode:YES completionBlock:^(CLLocation *location, AMapLocationReGeocode *regeocode, NSError *error) {
if (error) {
NSLog(@"%@", error.localizedDescription);
}
//打印定位到的位置坐标 location:(40.037471,116.370002)
NSLog(@"location:(%f,%f)", location.coordinate.latitude,location.coordinate.longitude);
if (regeocode) {
//反地理编码,打印定位到的地址信息 reGeocode:北京市海淀区东升镇庄盛百货(清河店)
NSLog(@"reGeocode:%@", regeocode.formattedAddress);
}
}];
网友评论