定位的基本使用

来源:互联网 发布:ubuntu使用数据库 编辑:程序博客网 时间:2024/06/06 03:37

iOS在实现定位功能前首先需要做两件事:

1、导入主头文件

 #import <CoreLocation/CoreLocation.h>
2、在info.plist 文件中添加权限

<key>NSLocationAlwaysUsageDescription</key>    <string>App需要您的同意,才能始终访问位置</string>

实现代码:

 

/*

 iOS8以前:系统会自动申请用户位置权限

 iOS8之后(包含iOS8.0): 必须要程序员去申请用户的位置权限

 

 */

#import "ViewController.h"

#import <CoreLocation/CoreLocation.h>

@interface ViewController ()<CLLocationManagerDelegate>

@property (nonatomic,strong) CLLocationManager *locationManager;

@end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    

    //  位置管理器常见属性

    //  当用户移动了多长的距离之后,才更新用户位置,单位(米)

    self.locationManager.distanceFilter = 10;

    //  定位精度,精度越高越费电

    self.locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;

    //    extern const CLLocationAccuracy kCLLocationAccuracyBestForNavigation; 导航使用最好进度

    //    extern const CLLocationAccuracy kCLLocationAccuracyBest;  最高精度

    //    extern const CLLocationAccuracy kCLLocationAccuracyNearestTenMeters;  10M

    //    extern const CLLocationAccuracy kCLLocationAccuracyHundredMeters; 100M

    //    extern const CLLocationAccuracy kCLLocationAccuracyKilometer; 1000M

    //    extern const CLLocationAccuracy kCLLocationAccuracyThreeKilometers; 3000M

    self.locationManager.activityType = CLActivityTypeFitness;

    //    CLActivityTypeOther = 1,

    //    CLActivityTypeAutomotiveNavigation,    // 汽车导航

    //    CLActivityTypeFitness,                // 步行导航

    //    CLActivityTypeOtherNavigation         // 其他导航,比如轮船,火车,飞机

    //

    

    if ([UIDevice currentDevice].systemVersion.doubleValue >= 8.0) {

        //      申请用户位置权限

        //      无论应用在前台还是在后台都需要使用用户的位置信息

        [self.locationManager requestAlwaysAuthorization];

        

    }else{

        //开始更新用户位置

        [self.locationManager startUpdatingLocation];

    }

}

#pragma mark - CLLocationManagerDelegate

//当授权状态发生改变了就调用该代理方法

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status

{

    //  当前请求到用户的权限后开始定位

    if (status == kCLAuthorizationStatusAuthorizedAlways) {

        [self.locationManager startUpdatingLocation];

    }else{

        NSLog(@"用户授权失败");

    }

}

//当更新用户位置的时候执行

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

{

    //距离当前时间最近的那个位置是这个数组的最后一个元素

    CLLocation *location = locations.lastObject;

    

    //地理编码器

    CLGeocoder *geocoder = [[CLGeocoder alloc] init];

    

    //反地理编码

    [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {

        //CLPlacemark 地标: 描述了地理的经纬度,已经地名,省,市,区,街道

        for (CLPlacemark *placemark in placemarks) {

            

            NSDictionary *addressDic = placemark.addressDictionary;

            

            NSString *state=[addressDic objectForKey:@"State"];

            

            NSString *city=[addressDic objectForKey:@"City"];

            

            NSString *subLocality=[addressDic objectForKey:@"SubLocality"];

            

            NSString *street=[addressDic objectForKey:@"Street"];

            

            NSLog(@"位置 = %@ %@ %@ %@", state, city, subLocality, street);

        }

    }];

    

}

/// 懒加载

- (CLLocationManager *)locationManager

{

    if (_locationManager == nil) {

        _locationManager = [[CLLocationManager alloc] init];

        _locationManager.delegate = self;

    }

    return _locationManager;

}

@end


在XCode中打印出: