• iOS CoreLocation 获取用户当前位置


    使用 CoreLocation获取用户当前位置

    1.导入系统库并初始化

    //定位
    #import <CoreLocation/CoreLocation.h>
    @interface XBMarkNumViewController ()<CLLocationManagerDelegate>//遵循协议CLLocationManagerDelegate
    @end
    @implementation XBMarkNumViewController{
        CLLocationManager *_locationManager;//定位服务管理类
        CLGeocoder * _geocoder;//初始化地理编码器
    }
    - (void)viewWillAppear:(BOOL)animated{
        [super viewWillAppear:animated];
        [self initLocationService];//初始化定位服务
    }

    //初始化定位服务:

    - (void)initLocationService {
        // 初始化定位管理器
        _locationManager = [[CLLocationManager alloc] init];
        [_locationManager requestWhenInUseAuthorization];
        //[_locationManager requestAlwaysAuthorization];//iOS8必须,这两行必须有一行执行,否则无法获取位置信息,和定位
        // 设置代理
        _locationManager.delegate = self;
        // 设置定位精确度到米
        _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        // 设置过滤器为无
        _locationManager.distanceFilter = kCLDistanceFilterNone;
        // 开始定位
        [_locationManager startUpdatingLocation];//开始定位之后会不断的执行代理方法更新位置会比较费电所以建议获取完位置即时关闭更新位置服务
        //初始化地理编码器
        _geocoder = [[CLGeocoder alloc] init];
    }

    //代理方法获取详细位置信息

    #pragma mark---------CLLocationManagerDelegate------
    - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
        XBLog(@"%lu",(unsigned long)locations.count);
        CLLocation * location = locations.lastObject;
        // 纬度
        CLLocationDegrees latitude = location.coordinate.latitude;
        // 经度
        CLLocationDegrees longitude = location.coordinate.longitude;
        XBLog(@"经度:%f,纬度:%f,海拔:%f,航向:%f,行走速度:%f", longitude, 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];
                XBLog(@"%@",placemark.name);
                //获取城市
                NSString *city = placemark.locality;
                if (!city) {
                    //四大直辖市的城市信息无法通过locality获得,只能通过获取省份的方法来获得(如果city为空,则可知为直辖市)
                    city = placemark.administrativeArea;
                }
                // 国家
                NSString * country = placemark.country;
                XBLog(@"国家,%@",country);
                // 省份
                NSString * province = placemark.administrativeArea;
                XBLog(@"省份,%@",province);
                //
                XBLog(@"市,%@",city);
                //
                NSString * block = placemark.subLocality;
                XBLog(@"区,%@",block);
                // 街道
                NSString * street = placemark.thoroughfare;
                XBLog(@"街道,%@",street);
                // 子街道
                NSString * subStreet = placemark.subThoroughfare;
                XBLog(@"子街道,%@",subStreet);
                // 位置名
                NSString * location = placemark.name;
                XBLog(@"位置名,%@",location);
                NSString * detail = [NSString stringWithFormat:@"%@%@%@%@%@%@",country,province,city,block,street,location];
                XBLog(@"您的当前位置是:%@",detail);
                DEFAULT_SET_OBJECT(detail, @"USER_LOCATION");
                
            }else if (error == nil && [placemarks count] == 0) {
                XBLog(@"No results were returned.");
            } else if (error != nil){
                XBLog(@"An error occurred = %@", error);
            }
        }];
        // [_locationManager stopUpdatingLocation];不用的时候关闭更新位置服务
    }

     

  • 相关阅读:
    1101-Trees on the Level
    1099-移动小球
    1096-组合数
    Windows环境配置Apache+Mysql+PHP
    ArtDialog简单使用示例
    实现数字与字母的随机数
    SQLServer2005:在执行批处理时出现错误。错误消息为: 目录名无效
    sql语句总结
    在SQL SErver中实现数组功能
    aspnet_regiis.exe 的用法
  • 原文地址:https://www.cnblogs.com/StevenHuSir/p/OC_GETUSERLOCATION.html
Copyright © 2020-2023  润新知