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 ...
随机推荐
- python 函数 参数 (难点传入dict list)
--使用参数组可以传值列表,字典:格式 #-*-coding:utf-8-*- def func5(x,*s,**gs): print(x) print(s) print(gs) print '*'* ...
- android自定义View_3——Optimizing the View
前言:现在你已经有了一个自定义的view了,并且支持手势和过度动画等属性,现在你要确认这个view 有够快的速度,为了使其在UI上不会显得迟钝或是卡停,你要保证view动画,能每秒最好有60帧. 一: ...
- UTI iPhone支持依文件后缀名打开应用
本文转载至 http://blog.csdn.net/zaitianaoxiang/article/details/6658492 iphoneapplicationprocessingfileurl ...
- Spring MVC的路径匹配
Spring MVC中的路径匹配比起标准web.xml的servlet映射要灵活得多.路径匹配的默认策略是由org.springframework.util.AntPathMatcher实现的.顾名思 ...
- SVG 与 Canvas:如何选择
SVG 与 Canvas:如何选择 61(共 69)对本文的评价是有帮助 - 评价此主题 本主题一开始将对 SVG 与 Canvas 进行简要比较,接下来会讨论大量的比较代码示例,如光线跟踪和绿屏 ...
- filter和find区别,元素遍历
转 filter和find区别 find()会在当前指定元素中查找符合条件的子元素,是对它的子集操作,而filter()则是在当前指定的元素集合中查找符合条件的元素,是对自身集合元素进行筛选. HTM ...
- PLSQL 用的让自己更舒心点!
每天开发时候 ,有时候会疲劳! 改下你的PLSQL 字体吧!
- 让vs只启动自己想调试的站点
VS中里面多个WEB项目如何只启动一个? 每次启动时,右下角都会出现一堆的 网站有10来个.即使设置了默认启动项目, 但每次按F5启动,或者哪怕是在项目上右键启动新实例 右下角都会出现这一堆的站点 有 ...
- Server Objects Extension(SOE)开发(三)
前言 SOE出现之前,一些复杂.耗时的gis操作,通常都是使用gp服务实现的.前面将gp服务和soe进行了对比分析,为了测试两种的效率,曾经做了个demo,使用soe和gp同时执行相同的业务逻辑,记录 ...
- PL/SQL 入门
1. 概述 PL/SQL(Procedure Language/SQL)是 Oracle 对 sql 语言的过程化扩展,指在 SQL 命令语言中增加了 过程处理语句(如分支,循环等),使 SQL 语言 ...