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 ...
随机推荐
- 我们可以用JAX-WS轻松实现JAVA平台与其他编程环境(.net等)的互操作
在 JAX-WS中,一个远程调用可以转换为一个基于XML的协议例如SOAP,在使用JAX-WS过程中,开发者不需要编写任何生成和处理SOAP消息的代码.JAX-WS的运行时实现会将这些API的调用转换 ...
- css 使图片紧贴底部显示
img{ display: table-cell; vertical-align: bottom; }
- 字符串匹配(KMP 算法 含代码)
主要是针对字符串的匹配算法进行解说 有关字符串的基本知识 传统的串匹配法 模式匹配的一种改进算法KMP算法 网上一比較易懂的解说 小样例 1计算next 2计算nextval 代码 有关字符串的基本知 ...
- Chromium开发--入门C++小样例
開始做Chromium开发的时候,都是从代码去摸索的,如今有几个样例了.希望看到的同学能有帮助,比直接在大量代码里领悟要快非常多了. C++ in Chromium 101 - Codelab h ...
- Ubuntu下安装phpMyAdmin
首先添加必要的apt源 1 apt-get update 确保软件包列表是最新的 apt-get upgrade 更新软件包 安装phpMyAdmin apt-get install phpmyadm ...
- [Spring Data Repositories]学习笔记--使用现有的repository
以下内容是在学习Spring-Data-mongoDB中的Spring Data Repositories时做的一些笔记.备忘! 感觉学习还是看官方的资料比较透彻一些. Spring Data Rep ...
- 【BZOJ4917】Hash Killer IV 乱搞
[BZOJ4917]Hash Killer IV Description 有一天,tangjz造了一个Hash函数: unsigned int Hash(unsigned int v){ un ...
- Mybatis Insert 返回主键ID
<insert id="insert" useGeneratedKeys="true" keyProperty="u_Id" para ...
- 安装git和配置
首先更新系统 yum -y update 安装依赖的包 yum -y install curl-devel expat-devel gettext-devel openssl-devel zli ...
- Selenium IDE的使用
Selenium IDE 的作用 Selenium IDE 是Firefox 浏览器的一个插件, 它会记录你对Firefox的操作,并且可以回放它的操作. 在实际自动化测试中,不会用Selenium ...