#import "ViewController.h" #import <CoreLocation/CoreLocation.h> @interface ViewController ()<CLLocationManagerDelegate> @property (weak, nonatomic) IBOutlet UITextField *longitude; @property (weak, nonatomic) IBOutlet UITextField *latitude; @property (weak, nonatomic) IBOutlet UITextField *height; @property (weak, nonatomic) IBOutlet UITextField *speed; @property (weak, nonatomic) IBOutlet UITextField *direction; @property(strong,nonatomic)CLLocationManager *locationManager; @end @implementation ViewController - (IBAction)beginLocation:(UIButton *)sender { if ([CLLocationManager locationServicesEnabled]) { NSLog(@"开始执行定位服务"); //设定定位精度:最佳精度 self.locationManager.desiredAccuracy = kCLLocationAccuracyBest; //设置距离过滤器为50米,表示每移动50米更新一次位置 self.locationManager.distanceFilter = 50; //将视图控制器自身设置为CLLocationManager的delegate //因此该视图控制器需要实现CLLocationManagerDelegate协议 self.locationManager.delegate = self; } else { NSLog(@"无法使用定位服务"); } } -(void) viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; //开启定位 [_locationManager startUpdatingLocation]; } -(void) viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; //结束定位 [_locationManager stopUpdatingLocation]; } #pragma mark - CLLocationDelegate代理协议 //成功获取定位数据后将会激发该方法 -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations { //获取最后一个定位数据 CLLocation *location = [locations lastObject]; //依次获取CLLocation中封装的经度、纬度、高度、速度、方向等信息 self.longitude.text = [NSString stringWithFormat:@"%g",location.coordinate.longitude];//纬度 NSLog(@"%f",location.coordinate.longitude); self.latitude.text = [NSString stringWithFormat:@"%g",location.coordinate.latitude];//经度 self.height.text = [NSString stringWithFormat:@"%g",location.altitude]; self.speed.text = [NSString stringWithFormat:@"%g",location.speed]; self.direction.text = [NSString stringWithFormat:@"%g",location.course]; } //定位失败执行的操作 -(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error { NSLog(@"定位失败:%@",error); } - (void)viewDidLoad { [super viewDidLoad]; //创建CLLocationManager对象 self.locationManager = [[CLLocationManager alloc]init]; } @end
运行效果图,如下:
#import "ViewController.h" #import <CoreLocation/CoreLocation.h> @interface ViewController ()<CLLocationManagerDelegate> { CALayer *znzLayer; } @property(strong,nonatomic)CLLocationManager *locationManager; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //如果磁力计可用,则开始监听方向改变 if ([CLLocationManager headingAvailable]) { //创建显示方向的指南针图片Layer; znzLayer = [[CALayer alloc]init]; NSInteger screenHeight = [UIScreen mainScreen].bounds.size.height; NSInteger y = (screenHeight - 320)/2; znzLayer.frame = CGRectMake(0, y, 320, 320); //设置znzLayer显示的图片 znzLayer.contents = (id)[[UIImage imageNamed:@"1.png"]CGImage]; //将znzLayer添加到系统的UIView中 [self.view.layer addSublayer:znzLayer]; //创建CLLocationManager对象 self.locationManager = [[CLLocationManager alloc]init]; self.locationManager.delegate = self; [self.locationManager startUpdatingHeading]; } else { [[[UIAlertView alloc]initWithTitle:@"提醒" message:@"您的设备不支持磁力计" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil]show]; } } #pragma mark - CLLocationManagerDelegate代理方法 -(void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading { //将设备的方向换算成弧度 CGFloat headings = -1.0f *M_PI * newHeading.magneticHeading / 180.0f; //创建不断改变CALayer的transform属性的属性动画 CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"transform"]; CATransform3D fromValue = znzLayer.transform; //设置动画开始的属性值 anim.fromValue = [NSValue valueWithCATransform3D:fromValue]; //绕Z轴旋转heading弧度的变换矩阵 CATransform3D toValue = CATransform3DMakeRotation(headings, 0, 0, 1); //设置动画结束时的属性 anim.toValue = [NSValue valueWithCATransform3D:toValue]; anim.duration = 0.5; anim.removedOnCompletion = YES; //设置动画结束后znzLayer的变换矩阵 znzLayer.transform = toValue; //为znzLayer添加动画 [znzLayer addAnimation:anim forKey:nil]; } -(BOOL)locationManagerShouldDisplayHeadingCalibration:(CLLocationManager *)manager { return YES; } @end
区域监测
#import "ViewController.h" #import <CoreLocation/CoreLocation.h> @interface ViewController ()<CLLocationManagerDelegate> @property(strong,nonatomic)CLLocationManager *locationManager; @end @implementation ViewController -(void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [self.locationManager startUpdatingLocation]; } - (void)viewDidLoad { [super viewDidLoad]; //判断是否开启定位服务 if ([CLLocationManager locationServicesEnabled]) { self.locationManager = [[CLLocationManager alloc]init]; //定义一个CLLocationCoordinate2D作为区域的圆心 CLLocationCoordinate2D companyCenter; companyCenter.latitude = 23.126272; companyCenter.longitude = 113.395568; //使用CLCircularRegion创建一个圆形区域,半径为500米 CLRegion *fkit = [[CLCircularRegion alloc]initWithCenter:companyCenter radius:500 identifier:@"fkit"]; //开始监听fkit区域 [self.locationManager startMonitoringForRegion:fkit]; self.locationManager.delegate = self; [self.locationManager requestAlwaysAuthorization]; } else { [[[UIAlertView alloc]initWithTitle:@"提醒" message:@"您的设备不支持定位" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil]show]; } [self.locationManager startUpdatingLocation]; } #pragma mark - CLLocationManagerDelegate代理方法 //进入指定区域以后弹出提示框提示用户 -(void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region { [[[UIAlertView alloc]initWithTitle:@"区域检测提示" message:@"您已经【进入】中关园区域" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]show]; } //离开指定区域以后将弹出提示框提示用户 -(void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region { [[[UIAlertView alloc]initWithTitle:@"区域检测提示" message:@"您已经【离开】中关园区域" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]show]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. }
原文:http://www.cnblogs.com/xjf125/p/5040116.html