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 ...
随机推荐
- asp.net c#采集需要登录页面的实现原理及代码
当我们采集页面的时候,如果被采集的网站需要登录才能采集,原理搞清楚了,就好办了,我们所要做的仅仅是在采集的时候(或者说HttpWebRequest提交数据的时候),将Cookie信息放入Http请求头 ...
- Uncaught Error: artDialog: Document types require more than xhtml1.0
这需要声明html文档,加上如下声明就不会报错了! <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ...
- 将场景导出XML或JSON或二进制而且解析还原场景
导出unity场景的全部游戏对象信息,一种是XML一种是JSON. 本篇文章我们把游戏场景中游戏对象的.旋转.缩放.平移与Prefab的名称导出在XML与JSON中.然后解析刚刚导出的XML或JSON ...
- Android OpenCV集成摄像头图片动态识别车牌号
最近两天开发一个使用OpenCV集成的一个识别车牌号的项目,困难重重,总结一下相关经验,以及开发注意事项: 一.开发环境: Android Studio 个人版本 3.1.4 NDK下载:14b CM ...
- 43、android:screenOrientation
android:screenOrientationThe orientation of the activity's display on the device. The value can be a ...
- mysql with python
前言: 数据库为人类解决了三大问题:持久化存储.优化读写.数据标准化. MySQL它不是数据库,它是管理数据库的软件.MySQL管理了很多数据库.是典型的服务型数据库,需要TCP/IP去连接. MyS ...
- 《从零开始学Swift》学习笔记(Day 42)——构造函数调用规则
原创文章,欢迎转载.转载请注明:关东升的博客 在构造函数中可以使用构造函数代理帮助完成部分构造工作.类构造函数代理分为横向代理和向上代理,横向代理只能在发生在同一类内部,这种构造函数称为便利构造函数. ...
- Android实现splash
笔者近日遇到一个android中双splash的问题.要求先实现百度的logo,在接入自己的logo. public class MainActivity extends BaseActivity { ...
- ECMAScript/JS 基础知识讲解
闭包 下面这个方法能输入0 1 2 ... 9吗?显然是不可以的,输出结果是10个10. function test() { var arr = []; for(var i = 0; i < 1 ...
- 为什么调用 GdiplusShutdown 函数会在 DllExports::GdipDeleteGraphics(nativeGraphics) 位置抛出异常?
因为没有仔细看文档 https://docs.microsoft.com/en-us/windows/desktop/api/Gdiplusinit/nf-gdiplusinit-gdiplusshu ...