[iOS]获取地理位置信息

1.在工程的 info.plist 文件中增加两个key( 右键 - Add Row )

Privacy - Location Always and When In Use Usage Description

Privacy - Location When In Use Usage Description

value里输入征求获取位置信息时展示的提示语:

2.在需要获取地理位置信息的文件中 

#import <CoreLocation/CoreLocation.h>

在interfce行添加相关的delegate 

CLLocationManagerDelegate

3.在interface构造两个属性:

@property (nonatomic,strong) CLLocationManager *locationManager;         //定位
@property (nonatomic,strong) CLGeocoder *geoCoder;                       //地理位置信息

 

4.初始化两个定位相关的工具类,我们把它写成一个 名叫 initLocationInfo 的函数:

*此处有个坑爹的地方,定位的获取必须在主线程中进行,不然的话虽然也会有弹窗提示看起来一切正常,但是不会触发delegate回调

- (void)initLocationInfo {
    // 初始化定位管理器
    _locationManager = [[CLLocationManager alloc] init];
    // 设置代理
    _locationManager.delegate = self;
    // 设置定位精确度到米
    _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    // 设置过滤器为无
    _locationManager.distanceFilter = kCLDistanceFilterNone;
    // 取得定位权限,有两个方法,取决于你的定位使用情况
    // 一个是requestAlwaysAuthorization,一个是requestWhenInUseAuthorization
    // 这句话ios8以上版本使用。
    [_locationManager requestAlwaysAuthorization];
    // 开始定位
    [_locationManager startUpdatingLocation];
    //地理信息
    _geoCoder = [[CLGeocoder alloc] init];
}

5.更新的地理位置信息会回调一个delegate:

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations

在回调的delegate中我们对回传的地理信息进行处理:

//MARK: - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
    NSLog(@"%lu",(unsigned long)locations.count);
    CLLocation * location = locations.lastObject;
    // 纬度
    CLLocationDegrees latitude = location.coordinate.latitude;
    // 经度
    CLLocationDegrees longitude = location.coordinate.longitude;
    NSLog(@"%@",[NSString stringWithFormat:@"%lf", location.coordinate.longitude]);
    //    NSLog(@"经度:%f,纬度:%f,海拔:%f,航向:%f,行走速度:%f", location.coordinate.longitude, location.coordinate.latitude,location.altitude,location.course,location.speed);
    
    [_geoCoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        if (placemarks.count > 0) {
            CLPlacemark *placemark = [placemarks objectAtIndex:0];
            NSLog(@"%@",placemark.name);
            //获取城市
            NSString *city = placemark.locality;
            if (!city) {
                //四大直辖市的城市信息无法通过locality获得,只能通过获取省份的方法来获得(如果city为空,则可知为直辖市)
                city = placemark.administrativeArea;
            }
            // 位置名
            NSLog(@"name,%@",placemark.name);
            // 街道
            NSLog(@"thoroughfare,%@",placemark.thoroughfare);
            // 子街道
            NSLog(@"subThoroughfare,%@",placemark.subThoroughfare);
            //
            NSLog(@"locality,%@",placemark.locality);
            //
            NSLog(@"subLocality,%@",placemark.subLocality);
            // 国家
            NSLog(@"country,%@",placemark.country);
        }else if (error == nil && [placemarks count] == 0) {
            NSLog(@"No results were returned.");
        } else if (error != nil){
            NSLog(@"An error occurred = %@", error);
        }
    }];
    //    [manager stopUpdatingLocation];不用的时候关闭更新位置服务,不关闭的话这个 delegate 隔一定的时间间隔就会有回调
}
posted @ 2020-01-13 11:29  Oran  阅读(2210)  评论(0编辑  收藏  举报