标签:distance pmon touch nsstring array net super 后台 ati
// // ViewController.m // CoreLoaction // // Created by on 2016/10/26. // Copyright ? 2016年 xiezefeng. All rights reserved. // #import "ViewController.h" #import <CoreLocation/CoreLocation.h> @interface ViewController ()<CLLocationManagerDelegate> @property (weak, nonatomic) IBOutlet UIImageView *myImageView; @property (weak, nonatomic) IBOutlet UILabel *cipianjiao; @property (weak, nonatomic) IBOutlet UILabel *detailText; @property (weak, nonatomic) IBOutlet UILabel *state; @property(nonatomic,strong) CLLocationManager * lm; @property(nonatomic,strong)CLLocation * oldLocation; @property (weak, nonatomic) IBOutlet UILabel *zouwei; @end @implementation ViewController - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{ NSLog(@"error.description %@",error.description); } -(CLLocationManager *)lm { // [_lm requestLocation]; if (!_lm) { //创建位置管理者 _lm=[[CLLocationManager alloc]init]; _lm.delegate = self; if ([[UIDevice currentDevice].systemVersion floatValue]>=8.0) { //ios 8 请求用户授权 // [self.lm requestWhenInUseAuthorization];//前台 [self.lm requestAlwaysAuthorization];//前后台 } // if ([_lm respondsToSelector:@selector(requestWhenInUseAuthorization)]) { // //ios 8 请求用户授权 //// [self.lm requestWhenInUseAuthorization];//前台 // [self.lm requestAlwaysAuthorization];//前后台 // } if ([_lm respondsToSelector:@selector(allowsBackgroundLocationUpdates)]) { //ios9 //允许后台定位 一定要够炫后台location updates self.lm.allowsBackgroundLocationUpdates= YES; } // _lm.distanceFilter = 100;//每隔多少米定位一次 _lm.desiredAccuracy = kCLLocationAccuracyBestForNavigation;//最好 jing } return _lm; } //授权状态发生改变导用 状态 -(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{ switch (status) { case kCLAuthorizationStatusNotDetermined: { _state.text=@"用户还没决定"; } break; case kCLAuthorizationStatusRestricted:{ _state.text=@"访问受限"; } case kCLAuthorizationStatusDenied: { if ([CLLocationManager locationServicesEnabled]) { NSLog(@"定位开启,但被拒"); }else{ _state.text=@"定位关闭,不可用"; } _state.text=@"用户还没决定"; break; } case kCLAuthorizationStatusAuthorizedAlways:{ _state.text=@"获得前后台定位授权"; } break; case kCLAuthorizationStatusAuthorizedWhenInUse:{ NSLog(@"获得前台定位授权"); _state.text=@"获得前台定位授权"; } default: break; } } -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ //更新用户位置 //默认前台 [self.lm startUpdatingLocation]; //手机方向变化监听 [self.lm startUpdatingHeading]; //区域监听 CLLocationCoordinate2D loact = CLLocationCoordinate2DMake(23.121481, 113.322436); CLCircularRegion * region =[[CLCircularRegion alloc]initWithCenter:loact radius:10000000 identifier:@"ident"]; [self.lm requestAlwaysAuthorization]; [self.lm startMonitoringForRegion:region]; [self.lm requestStateForRegion:region]; } -(void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(nonnull CLRegion *)region { } /** * 监听区域失败时调用 * * @param manager 位置管理者 * @param region 区域 * @param error 错误 */ -(void)locationManager:(CLLocationManager *)manager monitoringDidFailForRegion:(CLRegion *)region withError:(NSError *)error { // 经验: 一般在这里, 做移除最远的区域 // [manager stopMonitoringForRegion:@"最÷远区域"]; } //进入区域触发代理 -(void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region{ NSLog(@"进入区域"); UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"进入区域" message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles:@"ok", nil]; [alert show]; } -(void)locationManager:(CLLocationManager *)manager didExitRegion:(nonnull CLRegion *)region{ UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"离开区域" message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles:@"ok", nil]; [alert show]; NSLog(@"离开区域"); } - (void)viewDidLoad { [super viewDidLoad]; CLGeocoder * geoc = [CLGeocoder new]; // [geoc geocodeAddressString:@"广州" completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) { // // CLPlacemark * pm = placemarks[0]; // NSLog(@"%@",pm.name); // }]; // return; //地理反编码 CLLocation * locationt = [[CLLocation alloc]initWithLatitude:23.122392 longitude:113.319210]; [geoc reverseGeocodeLocation:locationt completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) { CLPlacemark * pm = placemarks[0]; NSLog(@"%@",pm.name); }]; self.view.backgroundColor = [UIColor orangeColor]; //停止定位 // [self.lm stopUpdatingLocation]; // Do any additional setup after loading the view, typically from a nib. } -(void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading{ //磁偏角 _cipianjiao.text = [NSString stringWithFormat:@"%f", newHeading.trueHeading]; CGFloat angle = newHeading.trueHeading; CGFloat angler =angle/100.0*M_PI; [UIView animateWithDuration:0.5 animations:^{ _myImageView.transform = CGAffineTransformMakeRotation(-angler); }]; } #pragma mark CLLocationManagerDelegate //更新到位置之后调用 int a; //locations 位置数组 -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{ CLLocation * location = locations.lastObject; NSLog(@"定位中%f %f %f",location.coordinate.latitude,location.coordinate.longitude,location.altitude); CLLocation * locationt = [[CLLocation alloc]initWithLatitude:23.122392 longitude:113.319210]; NSString * angleStr = nil; if (!_oldLocation) { _oldLocation = location; } switch ((int)_oldLocation.course/90) { case 0: angleStr = @"北偏东"; break; case 1: angleStr = @"东偏北"; break; case 2: angleStr = @"南偏西"; break; case 3: angleStr = @"西偏北"; break; default: angleStr = @"去哪了"; break; } if ((int)location%90==0) { NSRange range = NSMakeRange(0, 1); angleStr = [NSString stringWithFormat:@"正%@",[angleStr substringWithRange:(range)]]; } _zouwei.text = angleStr; //偏移角度 NSInteger angle = 0; angle =(int)location.course%90; //移动多少米 NSLog(@"========%f", [location distanceFromLocation:locationt]); _detailText.text = [NSString stringWithFormat:@"定位中%ld",(long)a++]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
标签:distance pmon touch nsstring array net super 后台 ati
原文地址:http://www.cnblogs.com/xiezefeng/p/6002772.html