1.CLLocation主要有一下几个重要的属性
/**
* CLLocation 详解
* coordinate : 经纬度
* altitude : 海拔
* course : 航向
* speed ; 速度
*/
1.1 distanceFromLocation: 方法
/*
* distanceFromLocation:
*
* Discussion:
* Returns the lateral distance between two locations.
*/
- (CLLocationDistance)distanceFromLocation:(const CLLocation *)location __OSX_AVAILABLE_STARTING(__MAC_10_6,__IPHONE_3_2);
CLLocation *l1 = [[CLLocation alloc] initWithLatitude:21.123 longitude:123.456];
CLLocation *l2 = [[CLLocation alloc] initWithLatitude:22.123 longitude:123.456];
CLLocationDistance distance = [l1 distanceFromLocation:l2];
NSLog(@"distance=%f", distance);
结果:
[3308:176001] distance=110725.220969
2.course :航向
/**
* 场景演示:打印当前用户的行走方向,偏离角度以及对应的行走距离,
例如:”北偏东 30度 方向,移动了8米”
*/
<pre name="code" class="objc">#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>
@interface ViewController ()<CLLocationManagerDelegate>
{
// 老位置
CLLocation *_oldL;
}
/** 位置管理者 */
@property (nonatomic, strong) CLLocationManager *lM;
@end
@implementation ViewController
#pragma mark - 懒加载
- (CLLocationManager *)lM
{
if (!_lM) {
// 1. 创建位置管理者
_lM = [[CLLocationManager alloc] init];
// 1.1 代理, 通知, block
_lM.delegate = self;
// 每隔多米定位一次
// _lM.distanceFilter = 100;
/**
kCLLocationAccuracyBestForNavigation // 最适合导航
kCLLocationAccuracyBest; // 最好的
kCLLocationAccuracyNearestTenMeters; // 10m
kCLLocationAccuracyHundredMeters; // 100m
kCLLocationAccuracyKilometer; // 1000m
kCLLocationAccuracyThreeKilometers; // 3000m
*/
// 精确度越高, 越耗电, 定位时间越长
_lM.desiredAccuracy = kCLLocationAccuracyBest;
/** -------iOS8.0+定位适配-------- */
if([[UIDevice currentDevice].systemVersion floatValue] >= 8.0)
{
// 前台定位授权(默认情况下,不可以在后台获取位置, 勾选后台模式 location update, 但是 会出现蓝条)
[_lM requestWhenInUseAuthorization];
// 前后台定位授权(请求永久授权)
// +authorizationStatus != kCLAuthorizationStatusNotDetermined
// 这个方法不会有效
// 当前的授权状态为前台授权时,此方法也会有效
// [_lM requestAlwaysAuthorization];
}
// 允许后台获取用户位置(iOS9.0)
if([[UIDevice currentDevice].systemVersion floatValue] >= 9.0)
{
// 一定要勾选后台模式 location updates
_lM.allowsBackgroundLocationUpdates = YES;
}
// if ([_lM respondsToSelector:@selector(requestAlwaysAuthorization)])
// {
// [_lM requestAlwaysAuthorization];
// }
}
return _lM;
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[self.lM startUpdatingLocation];
// CLLocation *l1 = [[CLLocation alloc] initWithLatitude:21.123 longitude:123.456];
// CLLocation *l2 = [[CLLocation alloc] initWithLatitude:22.123 longitude:123.456];
//
// CLLocationDistance distance = [l1 distanceFromLocation:l2];
// NSLog(@"distance=%f", distance);
}
#pragma mark - CLLocationManagerDelegate
/**
* 更新到位置之后调用
*
* @param manager 位置管理者
* @param locations 位置数组
* is kind of
*/
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations
{
// NSLog(@"定位到了");
/**
* CLLocation 详解
* coordinate : 经纬度
* altitude : 海拔
* course : 航向
* speed ; 速度
*/
CLLocation *location = [locations lastObject];
// NSLog(@"%@", location);
/**
* 场景演示:打印当前用户的行走方向,偏离角度以及对应的行走距离,
例如:”北偏东 30度 方向,移动了8米”
*/
// 1. 获取方向偏向
NSString *angleStr = nil;
switch ((int)location.course / 90) {
case 0:
angleStr = @"北偏东";
break;
case 1:
angleStr = @"东偏南";
break;
case 2:
angleStr = @"南偏西";
break;
case 3:
angleStr = @"西偏北";
break;
default:
angleStr = @"跑沟里去了!!";
break;
}
// 2. 偏向角度
NSInteger angle = 0;
angle = (int)location.course % 90;
// 代表正方向
if (angle == 0) {
NSRange range = NSMakeRange(0, 1);
angleStr = [NSString stringWithFormat:@"正%@", [angleStr substringWithRange:range]];
}
// 3.移动多少米
double distance = 0;
if(_oldL)
{
distance = [location distanceFromLocation:_oldL];
}
_oldL = location;
// 4. 拼串 打印
// 例如:”北偏东 30度 方向,移动了8米”
NSString *noticeStr = [NSString stringWithFormat:@"%@%zd方向, 移动了%f米", angleStr, angle, distance];
NSLog(@"%@", noticeStr);
}
/**
* 授权状态发生改变时调用
*
* @param manager 位置管理者
* @param status 状态
*/
-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
switch (status) {
// 用户还未决定
case kCLAuthorizationStatusNotDetermined:
{
NSLog(@"用户还未决定");
break;
}
// 问受限
case kCLAuthorizationStatusRestricted:
{
NSLog(@"访问受限");
break;
}
// 定位关闭时和对此APP授权为never时调用
case kCLAuthorizationStatusDenied:
{
// 定位是否可用(是否支持定位或者定位是否开启)
if([CLLocationManager locationServicesEnabled])
{
NSLog(@"定位开启,但被拒");
}else
{
NSLog(@"定位关闭,不可用");
}
// NSLog(@"被拒");
break;
}
// 获取前后台定位授权
case kCLAuthorizationStatusAuthorizedAlways:
// case kCLAuthorizationStatusAuthorized: // 失效,不建议使用
{
NSLog(@"获取前后台定位授权");
break;
}
// 获得前台定位授权
case kCLAuthorizationStatusAuthorizedWhenInUse:
{
NSLog(@"获得前台定位授权");
break;
}
default:
break;
}
}
// 定位失败
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"定位失败");
}
@end
3.指南针
3.1.磁北角度
- /**
- * CLHeading
- * magneticHeading : 磁北角度
- * trueHeading : 真北角度
- */
- #import "ViewController.h"
- #import <CoreLocation/CoreLocation.h>
- @interface ViewController ()<CLLocationManagerDelegate>
- /** 位置管理者 */
- @property (nonatomic, strong) CLLocationManager *lM;
- @property (weak, nonatomic) IBOutlet UIImageView *compassView;
- @end
- @implementation ViewController
- - (CLLocationManager *)lM
- {
- if (!_lM) {
- _lM = [[CLLocationManager alloc] init];
- _lM.delegate = self;
- // 每隔多少度更新一次
- _lM.headingFilter = 2;
- }
- return _lM;
- }
- - (void)viewDidLoad {
- [super viewDidLoad];
- // 开始监听设备朝向
- [self.lM startUpdatingHeading];
- }
- #pragma mark - CLLocationManagerDelegate
- /**
- * 获取到手机朝向时调用
- *
- * @param manager 位置管理者
- * @param newHeading 朝向对象
- */
- -(void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading
- {
- /**
- * CLHeading
- * magneticHeading : 磁北角度
- * trueHeading : 真北角度
- */
- NSLog(@"%f", newHeading.magneticHeading);
- CGFloat angle = newHeading.magneticHeading;
- // 把角度转弧度
- CGFloat angleR = angle / 180.0 * M_PI;
- // 旋转图片
- [UIView animateWithDuration:0.25 animations:^{
- self.compassView.transform = CGAffineTransformMakeRotation(-angleR);
- }];
- }
- @end
#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>
@interface ViewController ()<CLLocationManagerDelegate>
/** 位置管理者 */
@property (nonatomic, strong) CLLocationManager *lM;
@end
@implementation ViewController
- (CLLocationManager *)lM
{
if (!_lM) {
_lM = [[CLLocationManager alloc] init];
_lM.delegate = self;
//请求用户定位授权
if([[UIDevice currentDevice].systemVersion floatValue] >= 8.0)
{
[_lM requestAlwaysAuthorization];
}
}
return _lM;
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
// 区域监听
CLLocationCoordinate2D center = {21.13, 123.456};
CLCircularRegion *region = [[CLCircularRegion alloc] initWithCenter:center radius:1000 identifier:@"walden"];
// [self.lM startMonitoringForRegion:region];
// CLLocationCoordinate2D center2 = {21.13, 123.456};
// CLCircularRegion *region2 = [[CLCircularRegion alloc] initWithCenter:center2 radius:1000 identifier:@"walden"];
//
// [self.lM startMonitoringForRegion:region2];
// 请求区域状态
[self.lM requestStateForRegion:region];
}
#pragma mark - CLLocationManagerDelegate
// 进入区域
-(void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
{
NSLog(@"进入区域--%@", region.identifier);
}
// 离开区域
-(void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region
{
NSLog(@"离开区域--%@", region.identifier);
}
-(void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region
{
NSLog(@"%zd", state); //这个state是个枚举值
/**
* CLRegionState
*
* Discussion:
* Represents the current state of the device with reference to a region.
*
*
typedef NS_ENUM(NSInteger, CLRegionState){
CLRegionStateUnknown,
CLRegionStateInside,
CLRegionStateOutside
} NS_ENUM_AVAILABLE_IOS(7_0) __TVOS_PROHIBITED __WATCHOS_PROHIBITED;
*/
}
@end
5.地理编码与反地理编码
#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextView *addressTV;
@property (weak, nonatomic) IBOutlet UITextField *laTF;
@property (weak, nonatomic) IBOutlet UITextField *longTF;
/** 地理编码 */
@property (nonatomic, strong) CLGeocoder *geoC;
@end
@implementation ViewController
- (CLGeocoder *)geoC
{
if (!_geoC) {
_geoC = [[CLGeocoder alloc] init];
}
return _geoC;
}
- (IBAction)geoCoder {
NSString *addr = self.addressTV.text;
// if ([addr length] == 0) {
// return;
// }
[self.geoC geocodeAddressString:@"重庆" completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
/**
* CLPlacemark
location : 位置对象
addressDictionary : 地址字典
name : 地址全称
*/
if(error == nil)
{
NSLog(@"%@", placemarks);
[placemarks enumerateObjectsUsingBlock:^(CLPlacemark * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSLog(@"%@", obj.name);
self.addressTV.text = obj.name;
self.laTF.text = @(obj.location.coordinate.latitude).stringValue;
self.longTF.text = @(obj.location.coordinate.longitude).stringValue;
}];
}else
{
NSLog(@"cuowu--%@", error.localizedDescription);
}
}];
}
- (IBAction)reverseGeoCoder {
double lati = [self.laTF.text doubleValue];
double longi = [self.longTF.text doubleValue];
// TODO: 容错
CLLocation *loc = [[CLLocation alloc] initWithLatitude:lati longitude:longi];
[self.geoC reverseGeocodeLocation:loc completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
if(error == nil)
{
NSLog(@"%@", placemarks);
[placemarks enumerateObjectsUsingBlock:^(CLPlacemark * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSLog(@"%@", obj.name);
self.addressTV.text = obj.name;
self.laTF.text = @(obj.location.coordinate.latitude).stringValue;
self.longTF.text = @(obj.location.coordinate.longitude).stringValue;
}];
}else
{
NSLog(@"cuowu");
}
}];
}
@end
效果:
6. INTULocationManager框架的使用
#import "ViewController.h"
#import "INTULocationManager.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
#warning 注意要在info.plist中配置请求用户隐私权限的key
//精确度 超时 是否一直获取位置信息
// INTULocationManager *locMgr = [INTULocationManager sharedInstance];
// INTULocationRequestID requestID = [locMgr requestLocationWithDesiredAccuracy:INTULocationAccuracyCity
// timeout:5.0
// delayUntilAuthorized:YES
// block:^(CLLocation *currentLocation, INTULocationAccuracy achievedAccuracy, INTULocationStatus status) {
// if (status == INTULocationStatusSuccess) {
// NSLog(@"%@", currentLocation);
// }
// else
// {
// NSLog(@"cuowu--%zd", status);
// }
// }];
//
INTULocationManager *locMgr = [INTULocationManager sharedInstance];
INTULocationRequestID requestID = [locMgr subscribeToLocationUpdatesWithBlock:^(CLLocation *currentLocation, INTULocationAccuracy achievedAccuracy, INTULocationStatus status) {
if (status == INTULocationStatusSuccess) {
NSLog(@"%@", currentLocation);
}
else
{
NSLog(@"cuowu--%zd", status);
}
}];
// Force the request to complete early, like a manual timeout (will execute the block)
// [[INTULocationManager sharedInstance] forceCompleteLocationRequest:requestID];
// Cancel the request (won't execute the block)
// [[INTULocationManager sharedInstance] cancelLocationRequest:requestID];
}
@end