定位相关-CLLocationManager的使用。

#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>
@interface ViewController ()<CLLocationManagerDelegate>

@property(nonatomic,strong)CLLocationManager * manager;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    //    1.创建位置管理器 搞全局属性,移动要搞全局属性,否则无法使用位置管理器。
    self.manager = [CLLocationManager new];
    if ([self.manager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
        //    2.请求授权:用户使用的时候授权,配置plist, 一般要写,增加成功几率。
        //        用户使用的时候请求,大部分都只要执行这个就行了。
        [self.manager requestWhenInUseAuthorization];
        
        //    3.一直允许
//        [self.manager requestAlwaysAuthorization];
    }
        
    //    4.设置代理
    self.manager.delegate = self;
    
    //    4.开始定位
    [self.manager startUpdatingLocation];
    
    //    6.设置位置筛选,为了持续定位,少耗电,弄个属性,当位置改变之后再定位
    
    //    值为10.就代表位置变化超过十米再调动代理方法。(以米为单位的)此方法不打开就是一只调用代理方法。
//    self.manager.distanceFilter = 10;
    
    //    设置精准度 desiredAccuracy 精准度。
    //    定位:
    //    GPS 跟24颗卫星通讯。
    //    北斗 基站  Wi-Fi
    //    kCLLocationAccuracyBest (Xcode7之前这个值是默认的)
    //    kCLLocationAccuracyThreeKilometers 这个省电(当对位置的需求不是非常精确的)
//    self.manager.desiredAccuracy =kCLLocationAccuracyThreeKilometers;
    
    //    允许后台更新位置。需要配置plist,设置成功后屏幕上方会有蓝条,提示次程序正在定位。
//    self.manager.allowsBackgroundLocationUpdates = YES;
}


-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
    
    //CLLocationCoordinate2D coordinate 经纬度
    //CLLocationDegrees latitude      纬度
    //CLLocationDegrees longitude     经度    
//    位置对象
    CLLocation * location = locations[0];
    
    NSLog(@"%@",location);
    //    停止定位
    [self.manager stopUpdatingHeading];

    
}
@end

 

posted @ 2015-10-29 15:11  王刚韧(wanghy_iOS)  阅读(316)  评论(0编辑  收藏  举报