• iOS


    • 由于权限问题请在info.plist中加入以下两个键值对, 并且写上对应的描述信息
    • Privacy - Location When In Use Usage Description
    • Privacy - Location Always and When In Use Usage Description
    
    #import "ViewController.h"
    #import <CoreLocation/CoreLocation.h>
    @interface ViewController ()<CLLocationManagerDelegate>
    @property (nonatomic, strong) CLLocationManager *locationManager;
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        [self startLocation];
    }
    
    //开始定位
    - (void)startLocation {
        if ([CLLocationManager locationServicesEnabled]) {
            //        CLog(@"--------开始定位");
            self.locationManager = [[CLLocationManager alloc]init];
            self.locationManager.delegate = self;
            //控制定位精度,越高耗电量越
            self.locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;
            // 总是授权
            [self.locationManager requestAlwaysAuthorization];
            self.locationManager.distanceFilter = 10.0f;
            [self.locationManager requestAlwaysAuthorization];
            [self.locationManager startUpdatingLocation];
        }
    }
    
    - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
        if ([error code] == kCLErrorDenied) {
            NSLog(@"访问被拒绝");
        }
        if ([error code] == kCLErrorLocationUnknown) {
            NSLog(@"无法获取位置信息");
        }
    }
    //定位代理经纬度回调
    - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
        CLLocation *newLocation = locations[0];
        
        // 获取当前所在的城市名
        CLGeocoder *geocoder = [[CLGeocoder alloc] init];
        //根据经纬度反向地理编译出地址信息
        [geocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *array, NSError *error){
            if (array.count > 0){
                CLPlacemark *placemark = [array objectAtIndex:0];
                
                //获取城市
                NSString *city = placemark.locality;
                if (!city) {
                    //四大直辖市的城市信息无法通过locality获得,只能通过获取省份的方法来获得(如果city为空,则可知为直辖市)
                    city = placemark.administrativeArea;
                }
                NSLog(@"city = %@", city);//石家庄市
                NSLog(@"--%@",placemark.name);//某个街道某个号
                NSLog(@"++++%@",placemark.subLocality); //裕华区
                NSLog(@"country == %@",placemark.country);//中国
                NSLog(@"administrativeArea == %@",placemark.administrativeArea); //河北省
            }
            else if (error == nil && [array count] == 0)
            {
                NSLog(@"No results were returned.");
            }
            else if (error != nil)
            {
                NSLog(@"An error occurred = %@", error);
            }
        }];
        //系统会一直更新数据,直到选择停止更新,因为我们只需要获得一次经纬度即可,所以获取之后就停止更新
        [manager stopUpdatingLocation];
        
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    
    @end
    
    
  • 相关阅读:
    成立仅8个月的个人网站,月收入几十万美金
    Dynamics AX Bitmap to Image File
    孤儿药与长尾商品的网络营销
    How to control printer orientation(Landscape / Portrait) for an AX report in X++
    长城坑爹宽带,劫持用户DNS赚取购物返利
    2014年最大福利:185个Google排名因素!免费电子书下载
    火狐用户评价
    冲刺5
    《构建之法》阅读笔记03
    冲刺4
  • 原文地址:https://www.cnblogs.com/adampei-bobo/p/6552618.html
Copyright © 2020-2023  润新知