1.在高德地图官网上注册 成功开发者
2.添加一个应用 注册一个appKey
3.用cocoa pod 中 写入 pod 'AMapLocation' #定位SDK 下载SDK
- 需要在info.plist中追加 NSLocationWhenInUseUsageDescription 或NSLocationAlwaysUsageDescription 字段,以申请定位权限。
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
5.在appDelegate中 加入
import <AMapFoundationKit/AMapFoundationKit.h>
//注册 apiKey
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[AMapServices sharedServices].apiKey = @"29cd402f1007bb54d5e1f2857666c71f";
return YES;
}
- 在需要的地方 开始定位
#import <AMapLocationKit/AMapLocationKit.h>
@interface ViewController ()<AMapLocationManagerDelegate>
@property(nonatomic,strong)AMapLocationManager *manager;
@end
- (void)amapLocationManager:(AMapLocationManager *)manager didUpdateLocation:(CLLocation *)location
{
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
if (placemarks.count > 0) {
CLPlacemark *placemark = [placemarks objectAtIndex:0];
NSLog(@"%@",placemark);
//将获得的所有信息显示到label上
NSLog(@"placemark.name : %@", placemark.name);
//获取城市
NSLog(@"城市 = %@",placemark.locality);
//获取国家
NSLog(@"国家 = %@",placemark.country);
//获取省份
NSLog(@"省 = %@",placemark.administrativeArea);
self.resultLab.text = [NSString stringWithFormat:@"%@ %@ %@",placemark.country,placemark.administrativeArea,placemark.locality];
}
}];
}
- (IBAction)startLocation:(UIButton *)sender {
[self.manager startUpdatingLocation];
}
- (IBAction)endLocation:(UIButton *)sender {
[self.manager stopUpdatingLocation]; //停止持续定位
}