iOS_CLLocation定位


#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 = ;
//将视图控制器自身设置为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 - )/;
znzLayer.frame = CGRectMake(, y, , );
//设置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, , , );
//设置动画结束时的属性
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: 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.
}
iOS_CLLocation定位的更多相关文章
- Fis3的前端工程化之路[三大特性篇之资源定位]
Fis3版本:v3.4.22 Fis3的三大特性 资源定位:获取任何开发中所使用资源的线上路径 内容嵌入:把一个文件的内容(文本)或者base64编码(图片)嵌入到另一个文件中 依赖声明:在一个文本文 ...
- iPhone Anywehre虚拟定位提示“后台服务未启动,请重新安装应用后使用”的解决方法
问题描述: iPhone越狱了,之后在Cydia中安装Anywhere虚拟定位,但是打开app提示:后台服务未启动,请重新安装应用后使用. 程序无法正常使用... 解决方法: 打开Cydia-已安装, ...
- 网站定位之---根据IP获得区域
记得以前做一个培训机构网站时候需要定位,那时候用的搜狐的api,不是很精准. demo:https://github.com/dunitian/LoTCodeBase/tree/master/NetC ...
- CSS Position 定位属性
本篇文章主要介绍元素的Position属性,此属性可以设置元素在页面的定位方式. 目录 1. 介绍 position:介绍position的值以及辅助属性. 2. position 定位方式:介绍po ...
- CSS浮动、定位
这几天有空,整理了关于CSS浮动和定位的一些知识点,有什么欠缺的地方,欢迎大家批评指正. 一.文档流的概念指什么?有哪种方式可以让元素脱离文档流? 文档流,指的是元素排版布局过程中,元素会自动从左往右 ...
- JS实现页面进入、返回定位到具体位置
最为一个刚入职不久的小白...慢慢磨练吧... JS实现页面返回定位到具体位置 其实浏览器也自带了返回的功能,也就是说,自带了返回定位的功能.正常的跳转,返回确实可以定位,但是有些特殊场景就不适用了. ...
- 如何定位Oracle数据库被锁阻塞会话的根源
首先再次明确下,数据库因为要同时保证数据的并发性和一致性,所以操作有锁等待是正常的. 只有那些长时间没有提交或回滚的事物,阻塞了其他业务正常操作,才是需要去定位处理的. 1.单实例环境 2.RAC环境 ...
- iOS开发系列--地图与定位
概览 现在很多社交.电商.团购应用都引入了地图和定位功能,似乎地图功能不再是地图应用和导航应用所特有的.的确,有了地图和定位功能确实让我们的生活更加丰富多彩,极大的改变了我们的生活方式.例如你到了一个 ...
- 企业IT管理员IE11升级指南【16】—— 使用Compat Inspector快速定位IE兼容性问题
企业IT管理员IE11升级指南 系列: [1]—— Internet Explorer 11增强保护模式 (EPM) 介绍 [2]—— Internet Explorer 11 对Adobe Flas ...
随机推荐
- window7电脑git设置快捷命令
平常git开发的一些命令来回重复的敲,有点麻烦,这里给git的常用的命令设置了快捷键,很方便开发. 我这个是window7环境,下面开始 1.home键 + r 打开运行窗口,点击确定进入黑窗口 2. ...
- python3----splitlines
Python中的splitlines用来分割行.当传入的参数为True时,表示保留换行符 \n.通过下面的例子就很明白了: mulLine = """Hello!!! W ...
- Java_cpu飙升排查
1.现象 top 2.根据上图找到进程ID=28790 3.查找28790下线占用cpu高的线程ID -o THREAD,tid,time 4.根据上图发现线程ID=29161,换算成16进制 pri ...
- XML 文档的结构
XML 文档的组成 一个XML文档由两部分构成:第一部分是文档序言,第二部分是文档元素(节点). 1.文档序言 文档序言通常位于XML文档的顶端,根元素之前出现,它是一个特定的包含XML 文档设定信息 ...
- 1070 Bash游戏 V4
1070 Bash游戏 V4 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 有一堆石子共有N个.A B两个人轮流拿,A先拿.每次拿的数量最少1个,最多不超过对手上 ...
- EasyNVR智能云终端硬件使用说明(EasyNVR无插件直播服务硬件的具体使用方法)
问题背景 随着EasyNVR硬件版本(EasyNVR硬件云终端)的发布不少客户选择了EasyNVR云终端作为产品选择,在客户收到EasyNVR云终端的时候肯定都有一个疑问,那就是如何使用手头上的这个小 ...
- SharePoint服务器端对象模型 之 访问网站和列表数据(Part 5)
(五)列表条目(SPListItem) SharePoint中数据的存储基本上都是通过列表条目来完成(文档库中的文档也是一种特殊的列表条目),因此在SharePoint应用开发中,最终是要和列表条目打 ...
- 巨蟒django之CRM5 学习记录&&课程记录&&班级管理&&私户的数量上限
1.公户变私户(事务+行级锁) 2.私户的数量上限 3.班级的管理 4.课程记录管理 5.学习记录的初始化 6.展示和编辑学习记录
- Ubuntu 16.04安装各种软件
Ubuntu 16.04发布了,带来了很多新特性,同样也依然带着很多不习惯的东西,所以装完系统后还要进行一系列的优化. 1.删除libreoffice libreoffice虽然是开源的,但是Java ...
- Qt 如何像 VS 一样创建项目模版?
qt 存储模版路径位置:Qt\Qt5.9.5\Tools\QtCreator\share\qtcreator\templates\wizards 在里面随意复制一个模版,修改三项即可在 qt 中显示该 ...