• 定位城市的封装


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

    别忘了在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

  • 相关阅读:
    Linux进阶之bond链路聚合
    Linux服务之cobbler批量部署篇
    Linux进阶之排错
    shell基础之综合练习
    shell基础之99乘法表
    Linux进阶之VMware Linux虚拟机运行提示“锁定文件失败 虚拟机开启模块snapshot失败”的解决办法
    Linux服务之DNS服务篇
    linux服务之NTP及chrony时间同步
    八大排序算法汇总
    堆排序
  • 原文地址:https://www.cnblogs.com/dianming/p/6635676.html
Copyright © 2020-2023  润新知