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

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

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


//地理编码/逆地理编码
@property (nonatomic, retain) CLGeocoder *geocoder;
我把相关属性和方法封装在一个UIView的子类里面以上是.h 文件
下面是.m 文件 基本上想给地图定位或者需要获取城市名 的软件 这三个方法就可以满足
- (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 *)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];
}

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

#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=";
        //直辖市的原因 加上一个判断

此处应该格外住以如果想像我一样需要用 输出的城市名字的话 需要判断是否是直辖市 比如 北京市是用省的属性(

place.administrativeArea

)来输出的 市的属性(

place.locality

)就会为空 

以下是输出的具体的位置具体至街道号
  
        
        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);
    }];
}




你可能感兴趣的:(iOS)