iOS定位CLLocationManager以及CLLocationManagerDelegate协议的分享

//定位
@property (nonatomic, strong) CLLocationManager *locationManager;
@property (nonatomic, strong) NSString *cityName;

//得到的位置
@property (nonatomic, retain)  CLLocation *location;


//地理编码/逆地理编码
@property (nonatomic, retain) CLGeocoder *geocoder;
<span style="font-size:18px;">我把相关属性和方法封装在一个UIView的子类里面以上是.h 文件</span>
<span style="font-size:18px;">下面是.m 文件 基本上想给地图定位或者需要获取城市名 的软件 这三个方法就可以满足</span>
<pre name="code" class="objc">- (void)createLocationManager
{
    self.locationManager = [[CLLocationManager alloc] init];
    self.geocoder = [[CLGeocoder alloc] init];
   //初次运行会有系统的弹框 申请打开定位
    if (![CLLocationManager locationServicesEnabled]) {
        NSLog(@"定位服务当前可能尚未打开,请设置打开");
此处是未打开的状态 可以设置弹框来通知用户
        return ;
    }
    //如果没有授权则请求用户授权
    if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined) {
        [self.locationManager requestWhenInUseAuthorization];
    } else if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorizedWhenInUse) {
        //设置代理
        self.locationManager.delegate = self;(CLLocationManagerDelegate)
        //设置定位精度
        // 越高越费电
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        //定位频率,每隔多少米定位一次
        //        CLLocationDistance distance = 1000.0f;
        self.locationManager.distanceFilter = kCLDistanceFilterNone;
        //启动跟踪定位
        [self.locationManager startUpdatingLocation];
    }
    
}

#pragma 跟踪定位代理方法, 每次位置发生变化即执行 协议方法
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations
{
    self.location = [locations firstObject];//取出第一个位置
    CLLocationCoordinate2D coordinate = self.location.coordinate;//位置坐标
    
    NSLog(@"经度:%f,纬度:%f,海拔:%f,航向:%f,行走速度:%f",coordinate.longitude,coordinate.latitude,self.location.altitude,self.location.course,self.location.speed);
    
    [self getAddressByLatitude:coordinate.latitude longitude:coordinate.longitude];
    
    //如果不需要实时定位,使用完即使关闭定位服务
//想时时定位需要关掉此方法
    [_locationManager stopUpdatingLocation];
}

<span style="font-size:18px;">// 传的两个参数 第一个是纬度 第二个是经度 利用反地理编码来确定当前用户的位置
//CLGeocoder类用于处理地理编码和逆地理编码(又叫反地理编码)功能.</span>
//地理编码:根据给定的位置(通常是地名)确定地理坐标(经、纬度)。
//反地理编码:可以根据地理坐标(经、纬度)确定位置信息(街道、门牌等

#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 *place = [placemarks firstObject];
         NSString *str = @"http://apistore.baidu.com/microservice/weather?cityname=";
        //直辖市的原因 加上一个判断

<span style="font-size:24px;">此处应该格外住以如果想像我一样需要用 输出的城市名字的话 需要判断是否是直辖市 比如 北京市是用省的属性(</span><p style="margin-top: 0px; margin-bottom: 0px; font-size: 24px; line-height: normal; font-family: Menlo; color: rgb(0, 177, 255);"><span style="font-variant-ligatures: no-common-ligatures; color: #ffffff">place.</span>administrativeArea</p><span style="font-size:18px;">)来输出的 市的属性(</span><p style="margin-top: 0px; margin-bottom: 0px; font-size: 24px; line-height: normal; font-family: Menlo; color: rgb(0, 177, 255);"><span style="font-variant-ligatures: no-common-ligatures; color: #ffffff">place.</span>locality</p><span style="font-size:18px;">)就会为空 </span>

<span style="font-size:18px;">以下是输出的具体的位置具体至街道号</span>
  
        
        NSLog(@"%@", place.name);//位置名
        NSLog(@"thoroughfare,%@",place.thoroughfare);       // 街道
        NSLog(@"subThoroughfare,%@",place.subThoroughfare); // 子街道
        NSLog(@"locality,%@",place.locality);               // 市
        
        
        NSLog(@"subLocality,%@",place.subLocality);         // 区
        NSLog(@"country,%@",place.country);                 // 国家
        //  字典形式输出
        //  NSLog(@"详细信息:%@",placemark.addressDictionary);
    }];
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值