关于iOS开发首次进入需要获取地理位置

时间:2022-12-06 16:12:16

今天给大家简单介绍一下iOS开发过程中会遇到的获取地理位置的问题,(话不多说进入正题)这里给大家讲解一下两种在APPdelegate获取地理位置的方法:

一:首先是用系统的方法获取地理位置:

1、 首先在AppDelegate.m导入

@import CoreLocation;

@import MapKit;

2、 声明协议:

<CLLocationManagerDelegate> //协议

3、声明属性:

@property(nonatomic) CLLocationManager *locationManager;

4、

- (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;

}

5、完成地图的代理方法:

#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的内容进行讲解:

1、导入对应的头文件:

#import <AMapFoundationKit/AMapFoundationKit.h>

#import <AMapLocationKit/AMapLocationKit.h>

2、添加协议:

<AMapLocationManagerDelegate> //协议

3、声明属性:

@property (nonatomic,strong)AMapLocationManager * locationManager;

4、

- (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;

}

5、代理方法:

#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,或者本人技术不到位还没有找到解决的办法,望各位大神多多谅解和给出指导意见,谢谢!