- //
- // ViewController.m
- // CoreLocation
- //
- // Created by hq on 16/5/17.
- // Copyright © 2016年 hanqing. All rights reserved.
- //
- #import "ViewController.h"#import < CoreLocation / CoreLocation.h > @interface ViewController() < CLLocationManagerDelegate > @property(nonatomic, strong) CLLocationManager * clm;@property(nonatomic, strong) CLLocation * oldLocation; - (IBAction) getLocation;@end@implementation ViewController - (CLLocationManager * ) clm {
- if (_clm == nil) {
- _clm = [[CLLocationManager alloc] init];
- _clm.delegate = self;
- //定位精度
- /*
- 每隔多米定位一次
- _clm.distanceFilter = 100;
- kCLLocationAccuracyBestForNavigation // 最适合导航
- kCLLocationAccuracyBest; // 最好的
- kCLLocationAccuracyNearestTenMeters; // 10m
- kCLLocationAccuracyHundredMeters; // 100m
- kCLLocationAccuracyKilometer; // 1000m
- kCLLocationAccuracyThreeKilometers; // 3000m
- */
- // 精确度越高, 越耗电, 定位时间越长
- _clm.desiredAccuracy = kCLLocationAccuracyBest;
- //如果系统是ios 8.0以上
- if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
- //只有在使用的时候获取位置,在infor.plist需要配置文案NSLocationWhenInUseUsageDescription
- //[_clm requestWhenInUseAuthorization];
- //获取永久位置,在infor.plist需要配置文案NSLocationAlwaysUsageDescription
- [_clm requestAlwaysAuthorization];
- }
- //如果系统是ios 9.0
- if ([[UIDevice currentDevice].systemVersion floatValue] >= 9.0) {
- _clm.allowsBackgroundLocationUpdates = YES;
- }
- }
- return _clm;
- } - (void) viewDidLoad { [super viewDidLoad];
- }
- //获取位置
- - (IBAction) getLocation {
- [self.clm startUpdatingLocation];
- //[self.clm requestLocation];
- //[self.clm stopUpdatingLocation];
- }
- //实现代理中的监听方法
- - (void) locationManager: (CLLocationManager * ) manager didUpdateLocations: (NSArray < CLLocation * >*) locations {
- /**
- * CLLocation 详解
- * coordinate : 经纬度
- * altitude : 海拔
- * course : 航向
- * speed ; 速度
- */
- CLLocation * location = locations.lastObject;
- //获取经度纬度
- CLLocationCoordinate2D coordinate = location.coordinate;
- //获取行向(偏转角度)
- CLLocationDirection direction = location.course;
- NSString * dirString = nil;
- switch ((NSInteger)(direction) / 90) {
- case 0:
- dirString = @"北偏东";
- break;
- case 1:
- dirString = @"东偏南";
- break;
- case 2:
- dirString = @"南偏西";
- break;
- case 3:
- dirString = @"西偏北";
- break;
- }
- //获取偏转的角度
- NSInteger angle = (NSInteger)(direction) % 90;
- //判断是不是正北正南
- if (angle == 0) {
- NSRange range = NSMakeRange(0, 1);
- dirString = [dirString substringWithRange: range];
- }
- double distance = 0;
- if (self.oldLocation) {
- //获取移动的距离
- distance = [location distanceFromLocation: self.oldLocation];
- }
- self.oldLocation = location;
- NSLog(@"方向:%@ %ld度,走了%f", dirString, angle, distance);
- //NSLog(@"定位成功%ld--经度:%f 纬度: %f",locations.count,coordinate.longitude,coordinate.latitude);
- }
- //监听授权状态
- - (void) locationManager: (CLLocationManager * ) manager didChangeAuthorizationStatus: (CLAuthorizationStatus) status {
- switch (status) {
- case kCLAuthorizationStatusNotDetermined:
- NSLog(@"用户尚未选择");
- break;
- case kCLAuthorizationStatusRestricted:
- NSLog(@"反问首先");
- break;
- case kCLAuthorizationStatusDenied:
- //这种情况下,又分为两种情况
- //1 定位关闭,地理位置不可用
- if (! [CLLocationManager locationServicesEnabled]) {
- NSLog(@"用户未开启定位功能");
- } else {
- NSLog(@"用户拒绝授权");
- }
- break;
- case kCLAuthorizationStatusAuthorizedAlways:
- NSLog(@"获的永久授权");
- break;
- case kCLAuthorizationStatusAuthorizedWhenInUse:
- NSLog(@"只有在使用时获得授权");
- break;
- }
- } - (void) locationManager: (CLLocationManager * ) manager didFailWithError: (NSError * ) error {
- NSLog(@"定位失败");
- } - (void) didReceiveMemoryWarning { [super didReceiveMemoryWarning];
- }@end
来源: http://lib.csdn.net/snippet/ios/42602