关于CoreLocation的属性和基本的时候

本文详细介绍iOS定位技术的应用,包括如何实现用户位置的实时更新、不同精度级别的设置方法、以及iOS8及以上版本中用户授权的配置过程。此外还介绍了如何通过反编码地理位置来获取用户的详细地址。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

// 首先导入头文件#import<CoreLocation/CoreLocation.h>


/** * 定位管理者,全局变量强引用,防止销毁 */

@property (nonatomic ,strong) CLLocationManager *mgr;

// 2.成为CoreLocation管理者的代理监听获取到的位置self.mgr.delegate =self;

1需要得到用户的授权

ios8以上需要用户自己开启授权

在iOS8中不仅仅要主动请求授权,而且必须再info.plist文件中配置一项属性才能弹出授权窗口

  • NSLocationWhenInUseDescription,允许在前台获取GPS的描述
  • NSLocationAlwaysUsageDescription,允许在后台获取GPS的描述
  • iOS 9
  • 如果想要在后台定位,除了配置NSLocationAlwaysUsageDescription(前后台定位)外,还需要手动设置allowsBackgroundLocationUpdates = YES

  • ②开始用户定位

    - (void)startUpdatingLocation;
    

    ③停止用户定位

    - (void) stopUpdatingLocation;
    

    ③设置当用户移动多少米,重新定位

    - self.mgr.distanceFilter = 50;
    

    ④设置获取位置的精确度

    • 越精确就越耗电
        /*
      kCLLocationAccuracyBestForNavigation 最佳导航
      kCLLocationAccuracyBest;  最精准
      kCLLocationAccuracyNearestTenMeters;  10米
      kCLLocationAccuracyHundredMeters;  百米
      kCLLocationAccuracyKilometer;  千米
      kCLLocationAccuracyThreeKilometers;  3千米
         */
        self.mgr.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;

 初始化CLLocationManager 设置代理事件

 locationmanager =[[CLLocationManageralloc]init];

    locationmanager.delegate =self;

//次代码只为判断该程序是否开启定位

    CLAuthorizationStatus status =[CLLocationManagerauthorizationStatus];

    if (status ==kCLAuthorizationStatusNotDetermined) {

        NSLog(@"等待授权");

    }elseif(status ==kCLAuthorizationStatusAuthorizedAlways ||

             status == kCLAuthorizationStatusAuthorizedWhenInUse)

    {

        NSLog(@"授权成功");

    }else{

        NSLog(@"没有该权限");

    }


如果iOS系统为8.0以下 那么直接设置允许

if([[[UIDevicecurrentDevice] systemVersion]doubleValue]>8.0){

//这里是设置只有在运行该程序的时候使用定位系统,只有ios8才能设置此

[locationmanagerrequestWhenInUseAuthorization];

}

    //定位的频率也就是多少米定位一次

    _locationmanager.distanceFilter =20;

    //定位的精度

    _locationmanager.desiredAccuracy =kCLLocationAccuracyBest;

    

    //4.开始更新位置

    [_locationmanagerstartUpdatingLocation];

//该获取地理位置信息方法只有在手机上才可以测试

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{

    NSLog(@"find you");

    

    //获取用户最后的位置

    CLLocation *location = [locationslastObject];//位置信息

    CLLocationCoordinate2D coordinate = location.coordinate;//经纬度

    //打印出经纬度

    NSLog(@"lon:%f,lat:%f",coordinate.longitude,coordinate.latitude);

    //有时候找到用户位置后就不需要更新了

        [_locationmanagerstopUpdatingLocation];//停止位置更新

    //顺带添加一个反编码地理位置 但是不是太准确

CLLocation *mycll = [[CLLocation alloc] initWithLatitude:coordinate.latitude longitude:coordinate.longitude];

    //创建位置

    CLGeocoder *mycoder =[[CLGeocoder alloc]init];

    [mycoder reverseGeocodeLocation:mycll completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {

        if (!error&& [placemarks count] > 0) {

            NSDictionary *dict =[[placemarks objectAtIndex:0]addressDictionary];

            NSLog(@"street address: %@",

                  //记录地址

                  [dict objectForKey:@"Street"]);

        }

    }];


}

-(void)locationManager:(nonnullCLLocationManager *)manager didFailWithError:(nonnullNSError *)error{

    NSLog(@"not find");

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值