【iOS】CoreLocation相关知识

通过CoreLocation框架来获得定位信息,还是比较简单的,现总结如下:

1、引入CoreLocation框架

利用iOS7新特性,我们不需像之前那样“target -> Build Phases ->Link Binary With Libraries”来导入框架,我们只需在需要用到定位的类中,这样引入:

@import CoreLocation;  

这被称为“Modules”.

2、遵守CLLocationManagerDelegate协议

遵守CLLocationManagerDelegate协议

3、始于iOS8的分歧

iOS8之后,我们需要在APP的plist文件中增加两个字段,才能调用定位,如图:

plist文件中增加两个字段

这两个字段对应的Value可以为空,也可以自定义,自定义的文本内容会在请求授权的时候,展示在alertview中。

增加这两个字段,可能是为了方便系统或者第三方SDK检测你的APP是否使用了定位。
iOS8之前,并不强制添加这两个字段。
接下来,我们开始实例化一个CLLocationManager,这在iOS8前后略有区别。
iOS8之前:

self.locationManager = [[CLLocationManager alloc]init];
self.locationManager.delegate = self;
//选择定位的方式为最优的状态,他有四种方式在文档中能查到
 self.locationManager.desiredAccuracy=kCLLocationAccuracyBest;
            
[self getLocation];  

iOS8之后:

self.locationManager = [[CLLocationManager alloc]init];
//iOS8需要设置授权方式                
//用到定位的时候授权                
[self.locationManager requestWhenInUseAuthorization];
//或者选择这种,不用定位时也授权                
//[self.locationManager requestAlwaysAuthorization];
               
                
                
 self.locationManager.delegate = self;
 //选择定位的方式为最优的状态,他有四种方式在文档中能查到
 self.locationManager.desiredAccuracy=kCLLocationAccuracyBest;
                
[self getLocation];  

4、开始定位:

-(NSString*)getLocation
{
    
        [self.locationManager startUpdatingLocation];
        NSString* latitude = [NSString stringWithFormat:@"%f,",_locationManager.location.coordinate.latitude];
        NSString* longitude  = [NSString stringWithFormat:@"%f",_locationManager.location.coordinate.longitude];
        NSString* location = [NSString stringWithFormat:@"%@%@",latitude,longitude];
        return location;
}  

当locationManager调用startUpdatingLocation方法之后,CLLocationManagerDelegate的相关方法会执行。根据代理方法作相应的回调。
比如:



#pragma mark - CLLocationManagerDelegate

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
    if (status == kCLAuthorizationStatusDenied) {
        self.location = @"";
        [self performSelectorInBackground:@selector(archiveClientData) withObject:nil];
        [manager stopUpdatingLocation];
        self.locationManager.delegate = nil;
        
    }else if (status == kCLAuthorizationStatusAuthorized){
        self.location = [self getLocation];
        
        [self performSelectorInBackground:@selector(archiveClientData) withObject:nil];
        [manager stopUpdatingLocation];
        self.locationManager.delegate = nil;
    }
    
    if([[[UIDevice currentDevice]systemVersion]floatValue]>=8.0){
        if (status == kCLAuthorizationStatusAuthorizedWhenInUse){
            self.location = [self getLocation];
            
            [self performSelectorInBackground:@selector(archiveClientData) withObject:nil];
            [manager stopUpdatingLocation];
            self.locationManager.delegate = nil;
        }
    }
    
}  

记得在这些定位授权结束后,关掉定位,调用stopUpdatingLocation,这样APP会更节能。

定位的基本使用就是这样简单。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容