今天給你們簡單介紹一下iOS開發過程當中會遇到的獲取地理位置的問題,(話很少說進入正題)這裏給你們講解一下兩種在APPdelegate獲取地理位置的方法:ios
一:首先是用系統的方法獲取地理位置:git
一、 首先在AppDelegate.m導入api
@import CoreLocation;app
@import MapKit;ide
二、 聲明協議:學習
<CLLocationManagerDelegate> //協議ui
三、聲明屬性:編碼
@property(nonatomic) CLLocationManager *locationManager;atom
四、線程
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.locationManager = [[CLLocationManager alloc] init];
//判斷異常
if (![CLLocationManager locationServicesEnabled]) {
NSLog(@"定位服務不可用!");
}
//定位精度
_locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.delegate = self;
//啓動定位
[_locationManager startUpdatingLocation];
....
return YES;
}
五、完成地圖的代理方法:
#pragma mark -- CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray<CLLocation *> *)locations {
static BOOL isLocation = NO;
//地理位置逆編碼
[self veverseGeLocation:manager.location completion:^(BOOL sucess, id content) {
if (sucess) {
//這裏打印一下數據,方便查看
NSLog(@"%@",content);
//NSString * provnice = content[@"State"];
//NSString * city = content[@"City"];
//NSString * district = content[@"SubLocality"];
if (isLocation == YES) {
return ;
}
//若是你其餘界面須要用到該地理位置;則直接將NSString 內容保存下來,須要在取出來
isLocation = YES;
[self.locationManager stopUpdatingLocation];
}else{
}
}];
//中止定位
[manager stopUpdatingLocation];
}
//地理位置逆編碼:把經緯度信息編碼成格式化的地理位置信息
- (void)veverseGeLocation:(CLLocation *)location completion:(void(^)(BOOL sucess, id content)) completion{
CLGeocoder *coder = [[CLGeocoder alloc]init];
//逆編碼方法,後臺線程執行
[coder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
BOOL sucess;
id content = nil ;
if (error) {
sucess = NO;
content = error.localizedDescription;
}
else{
sucess = YES;
CLPlacemark *placeMark = placemarks.firstObject;
content = placeMark.addressDictionary;
}
completion(sucess,content);
}];
}
二:首先是用高德的方法獲取地理位置:用高德地圖的話首先須要添加開發地圖須要用到的庫:對於庫的導入依據開發人員的實際需求而定進行cocoapods導入或者手動拖入,前面流程基本都是跟隨高德地圖api 進行的,再次就很少重複介紹,詳情各位可自行前往學習(連接:http://lbs.amap.com/api/ios-sdk/guide/create-project,下面就APPdelegate的內容進行講解:
一、導入對應的頭文件:
#import <AMapFoundationKit/AMapFoundationKit.h>
#import <AMapLocationKit/AMapLocationKit.h>
二、添加協議:
<AMapLocationManagerDelegate> //協議
三、聲明屬性:
@property (nonatomic,strong)AMapLocationManager * locationManager;
四、
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[AMapServices sharedServices].apiKey = @"您的key";
[[AMapServices sharedServices] setEnableHTTPS:YES];
//建立地圖manager
self.locationManager = [[AMapLocationManager alloc] init];//實例化位置管理器
[self.locationManager setDelegate:self];
[self.locationManager startUpdatingLocation];//啓動定位
[self.locationManager setPausesLocationUpdatesAutomatically:NO];
[self.locationManager setDesiredAccuracy:kCLLocationAccuracyBest];//定位精度
self.locationManager.locatingWithReGeocode = YES;//連續定位是否返回逆地理信息,默認NO。
return YES;
}
五、代理方法:
#pragma mark - AMapLocationManager Delegate
- (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);
static BOOL isLocation = NO;
if (reGeocode != nil && isLocation == NO) {
// NSString * provnice = reGeocode.province;
// NSString * city = reGeocode.city;
// NSString * district = reGeocode.district;
// NSString *codeStr = reGeocode.adcode;//地理adcode
//若是你其餘界面須要用到該地理位置;則直接將NSString 內容保存下來,須要在取出來
isLocation = YES;
}
}
以上就是二者獲取地理位置的方法,須要說明的是:若是開發項目中後面會用到地理位置的adcode的時候,本人建議用第二種方法,由於第一種目前不能獲取到該adcode,或者本人技術不到位尚未找到解決的辦法,望各位大神多多諒解和給出指導意見,謝謝!