关于IOS定位和CLLocationManager 小知识点总结

来源:互联网 发布:计算机常用端口 编辑:程序博客网 时间:2024/06/05 00:28

  

1.判断是否开启定位

    if (![CLLocationManager locationServicesEnabled ]) {

     //判断是否开启定位
        NSLog(@"请开启定位 ");
    }


2.判断使用的时候,才开始开启定位
    if ([CLLocationManager authorizationStatus] != kCLAuthorizationStatusAuthorizedWhenInUse) {
        
        //请求用户授权 (退到后台也会也会定位)
       //    [self.mgr requestAlwaysAuthorization];
        
        //当用户使用的时候,才会定位
        [self.mgr requestWhenInUseAuthorization]; //对应在App中,NSLocationWhenInUseUsageDescription
    }

3.开始定位
    [self.mgr startUpdatingLocation];


4.在懒加载中,常见的属性

-(CLLocationManager *)mgr{
    if (!_mgr) {
        _mgr = [[CLLocationManager alloc]init];
        
        
        //定位精度 (最精度)
        _mgr.desiredAccuracy = kCLLocationAccuracyBest;
        
        //跟新的最小距离
        _mgr.distanceFilter = 100;
        
        
        //代理
        _mgr.delegate = self;
    }
    return _mgr ;
    }


5.计算两点的距离  
-(void)corlocation{
    //北京
    CLLocation *beijing = [[CLLocation alloc]initWithLatitude:39.54 longitude:116.28 ];
    
    //深圳
    CLLocation *shenzheng = [[CLLocation alloc]initWithLatitude:22.27 longitude:113.46];
    
    //两点之间的距离
    CLLocationDistance distance =  [beijing distanceFromLocation:shenzheng];
    
    //打印
    NSLog(@" 北京到深圳的距离   %f",distance);
}

6.常见的代理方法

/** (耗电)
 *  当获取到用户的位置时候调用
 *
 *  @param manager   manager description
 *  @param locations 用户位置
 */
-(void)locationManager:(nonnull CLLocationManager *)manager didUpdateLocations:(nonnull NSArray *)locations{
 
    //获取用的最新位置
    CLLocation  *userLocal = [locations lastObject];
    
    //停止定位
    [self.mgr stopUpdatingLocation];
    
    //纬度
    CLLocationDegrees latitude = userLocal.coordinate.latitude;
    
    //经度
    CLLocationDegrees longitude = userLocal.coordinate.longitude;
    
    NSLog(@" 纬度 %lf , 经度 %lf",latitude,longitude);
}

7.自从ios8,开发这在使用的定位功能之前,需要在info.plist里面添加(以下二选一,两个都添加默认使用NSLocationWhenInUseUsageDescription)

NSLocationAlwaysUsageDescription  (允许永久,包括后台也使用,使用GPS的描述)
NSLocationWhenInUseUsageDescription (当使用App时,才会调用定位)

8.在定位中要使用的几种状态解释

kCLAuthorizationStatusNotDetermined   用户尚未做出决定是否启用定位服务
kCLAuthorizationStatusRestrictec   没有获取用户授权使用定位服务,可能用户没有自己禁止访问授权
kCLAuthorizationStatusDenied   用户已经明确禁止应用使用定位服务或者当钱系统定位服务处于关闭状态
kCLAuthorizationStatusAuthorizedAlways  应用获得授权可以一直使用定位服务,即使应用不在使用状态。
kCLAuthorizationStatusAuthorizedWhenInUse  使用此应用过程中运行访问定位服务

9.定位精度 (最精度

  例子: _mgr.desiredAccuracy  =  kCLLocationAccuracyBest;
        
 //五种定位精度:  
 kCLLocationAccuracyBest;  //最精准定位
 kCLLocationAccuracyNearestTenMeters; //十米误差
 kCLLocationAccuracyHundredMeters; //百度误差范围
 kCLLocationAccuracyKilometer; //千米误差范围
 kCLLocationAccuracyThreeKilometers; //三千米误差范围


0 0