美文网首页
学习笔记:CLLocationManager

学习笔记:CLLocationManager

作者: _sun1993 | 来源:发表于2017-03-29 19:37 被阅读0次

    通过懒加载创建LocationManager:
    // 懒加载

    • (CLLocationManager *)locationManager
      {
      if (_locationManager == nil) {
      // 创建
      _locationManager = [[CLLocationManager alloc] init];
      // 设置代理
      _locationManager.delegate = self;
      // 每隔多少M 定位一次
      // _locationManager.distanceFilter = 100;

        /**
         kCLLocationAccuracyBestForNavigation // 最适合导航
         kCLLocationAccuracyBest; // 最好的
         kCLLocationAccuracyNearestTenMeters; // 10m
         kCLLocationAccuracyHundredMeters; // 100m
         kCLLocationAccuracyKilometer; // 1000m
         kCLLocationAccuracyThreeKilometers; // 3000m
         */
        // 精度越高越耗电
        _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        // 当使用的时候
        [_locationManager requestWhenInUseAuthorization];
        // 一直使用
      

    // [_locationManager requestAlwaysAuthorization];

    }
    return _locationManager;
    

    }

    locationManager的代理方法
    // 获取地理位置

    • (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation > )locations
      {
      NSLog(@"定位到了");
      NSLog(@"%@",locations.lastObject);
      /
      • CLLocation 详解
      • coordinate : 经纬度
      • altitude : 海拔
      • course : 航向
      • speed ; 速度
        */
        CLLocation *location = locations.lastObject;

    // CLLocation *locationNew = [CLLocation alloc] initWithLatitude: longitude:<#(CLLocationDegrees)#>
    [self.geocoder reverseGeocodeLocation:locations.lastObject completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {

        CLPlacemark *placemark = placemarks.firstObject;
        NSLog(@"%@",placemark.addressDictionary);
        self.cityLabel.text = placemark.addressDictionary[@"City"];
        self.streetLabel.text = placemark.addressDictionary[@"Street"];
        self.guizhidaoLabel.text = placemark.addressDictionary[@"Name"];
    }];
    

    }
    // 授权发生改变时会调用

    • (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
      {
      switch (status) {
      // 用户还未决定
      case kCLAuthorizationStatusNotDetermined:
      {
      NSLog(@"用户还未决定");
      break;
      }
      // 访问受限
      case kCLAuthorizationStatusRestricted:
      {
      NSLog(@"访问受限");
      break;
      }
      // 定位关闭时和对此APP授权为never时调用
      case kCLAuthorizationStatusDenied:
      {
      // 定位是否可用(是否支持定位或者定位是否开启)
      if([CLLocationManager locationServicesEnabled])
      {
      NSLog(@"定位开启,但被拒");
      }else
      {
      NSLog(@"定位关闭,不可用");
      }
      // NSLog(@"被拒");
      break;
      }
      // 获取前后台定位授权
      case kCLAuthorizationStatusAuthorizedAlways:
      // case kCLAuthorizationStatusAuthorized: // 失效,不建议使用
      {
      NSLog(@"获取前后台定位授权");
      break;
      }
      // 获得前台定位授权
      case kCLAuthorizationStatusAuthorizedWhenInUse:
      {
      NSLog(@"获得前台定位授权");
      break;
      }
      default:
      break;
      }

    }

    pragma mark 根据地名确定地理坐标

    -(void)getCoordinateByAddress:(NSString *)address{
    //地理编码
    [_geocoder geocodeAddressString:address completionHandler:^(NSArray *placemarks, NSError *error) {
    //取得第一个地标,地标中存储了详细的地址信息,注意:一个地名可能搜索出多个地址
    CLPlacemark *placemark=[placemarks firstObject];

        CLLocation *location=placemark.location;//位置
        CLRegion *region=placemark.region;//区域
        NSDictionary *addressDic= placemark.addressDictionary;//详细地址信息字典,包含以下部分信息
    

    // NSString *name=placemark.name;//地名
    // NSString *thoroughfare=placemark.thoroughfare;//街道
    // NSString *subThoroughfare=placemark.subThoroughfare; //街道相关信息,例如门牌等
    // NSString *locality=placemark.locality; // 城市
    // NSString *subLocality=placemark.subLocality; // 城市相关信息,例如标志性建筑
    // NSString *administrativeArea=placemark.administrativeArea; // 州
    // NSString *subAdministrativeArea=placemark.subAdministrativeArea; //其他行政区域信息
    // NSString *postalCode=placemark.postalCode; //邮编
    // NSString *ISOcountryCode=placemark.ISOcountryCode; //国家编码
    // NSString *country=placemark.country; //国家
    // NSString *inlandWater=placemark.inlandWater; //水源、湖泊
    // NSString *ocean=placemark.ocean; // 海洋
    // NSArray *areasOfInterest=placemark.areasOfInterest; //关联的或利益相关的地标
    NSLog(@"位置:%@,区域:%@,详细信息:%@",location,region,addressDic);
    }];
    }

    pragma mark 根据坐标取得地名

    -(void)getAddressByLatitude:(CLLocationDegrees)latitude longitude:(CLLocationDegrees)longitude{
    //反地理编码
    CLLocation *location=[[CLLocation alloc]initWithLatitude:latitude longitude:longitude];
    [_geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
    CLPlacemark *placemark=[placemarks firstObject];
    NSLog(@"详细信息:%@",placemark.addressDictionary);
    }];
    }

    相关文章

      网友评论

          本文标题:学习笔记:CLLocationManager

          本文链接:https://www.haomeiwen.com/subject/adecottx.html