1.在高德地圖官網(wǎng)上注冊 成功開發(fā)者
2.添加一個應(yīng)用 注冊一個appKey
3.用cocoa pod 中 寫入 pod 'AMapLocation' #定位SDK 下載SDK
- 需要在info.plist中追加 NSLocationWhenInUseUsageDescription 或NSLocationAlwaysUsageDescription 字段,以申請定位權(quán)限皆串。
<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]; //停止持續(xù)定位
}