定位城市的封装

这里只是定位当前所在的城市,所以用原生的就可以. 

别忘了在info.plist添加对应定位的key.

 

不扯淡,上代码:

@interface GpsManager : NSObject<CLLocationManagerDelegate>

@property (nonatomic,strong)CLLocationManager *manager;

@property (nonatomic,copy)NSString *cityName;


+ (GpsManager *)sharedGpsManager;

+ (BOOL)cdm_isLocationServiceOpen;

- (void)cdm_getGps;
- (void)cdm_stop;

 

核心代码:

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations
{
    CLLocation *location = [locations lastObject];
   // NSLog(@"%zd",location.coordinate);
    
    // 反向地理编译出地址信息
    [self.geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        if (!error)
        {
            if ([placemarks count] > 0) {
                
                CLPlacemark *placemark = [placemarks firstObject];
               // NSLog(@"placemark-----%@",placemark);
                NSLog(@"placemark.city-----%@",placemark.locality);
                
                // 获取城市
                NSString *city = placemark.locality;
                if (!city.length) {
                    // 6
                    [GpsManager sharedGpsManager].cityName = @"北京";
                }
                
                else {
                    [GpsManager sharedGpsManager].cityName = city;
                }
                
                
              //  NSLog(@"self.cityName---%@",[GpsManager sharedGpsManager].cityName);
                
            } else if ([placemarks count] == 0) {
                
                
                [GpsManager sharedGpsManager].cityName = @"北京";
            }
        }
        else
        {
            [GpsManager sharedGpsManager].cityName = @"北京";
        }
        
        //        [[CKGpsManager sharedGpsManager] stop];
        //1.这种是在需要的时候用,和下面NSUserDefaults 看自己的需求来用.
        [[NSNotificationCenter defaultCenter] postNotificationName:@"GPS" object:nil userInfo:@{@"cityName":[GpsManager sharedGpsManager].cityName}];
        
        NSString *tmpCity = [GpsManager sharedGpsManager].cityName;
        tmpCity = [tmpCity stringByReplacingOccurrencesOfString:@"" withString:@""];
        
        //保存当前位置信息
        if (!tmpCity.length) {
            tmpCity = @"北京";
        }
        //2.这种是在首页或者启动的时候用.
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        [defaults setObject:tmpCity forKey:@"CurrentCityKey"];
        [defaults synchronize];
    }];
    
    [self.manager stopUpdatingLocation];
    
}

 

调用:

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //1.这种直接加入直接在首页的时候就已经获取定位拿到当前城市,在你要用的地方直接取.
    NSLog(@"gps----%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"CurrentCityKey"]);
    self.view.backgroundColor = [UIColor orangeColor];
    
    //2.有一种情况是某些APP当你用到的定位的时候才回去提示用户,获取当前城市,可以用下面这种.
    [[GpsManager sharedGpsManager] cdm_getGps];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getGpsMsg:) name:@"GPS" object:nil];
    // Do any additional setup after loading the view.
}

- (void)getGpsMsg:(NSNotification *)noti
{
    NSLog(@"%@",noti.userInfo[@"cityName"]);
}

效果展示(详细请看Demo):

 

Demo地址:https://github.com/domanc/GpsManager.git

 

posted @ 2017-03-28 16:55  iOS_Doman  阅读(722)  评论(0编辑  收藏  举报