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 ...
随机推荐
- 004androidStudio ndk开发环境
004androidStudio ndk开发环境 android studio中编译C/C++源代码 1. 配置ndk.dir 在 local.properties 添加如下配置: sdk.dir=p ...
- BestCoder Round #93 ABC
A: 题目大意: 将数组划分成最少的段,每段的数两两不同. 题解:直接用一个map记录一个数是否出现过,贪心的每次取最多个数就好. B: 题目大意: 给出一个0-9组成的字符串,问能否删掉K个数字,使 ...
- bat遍历当前目录下的文件,批量重命名
@echo off setlocal enabledelayedexpansion for %%x in (*) do ( if not "%%x"=="demo.bat ...
- 使用spring+quartz配置多个定时任务
Spring被用在了越来越多的项目中, quartz也被公认为是比较好用的定时器设置工具, 在这里通过一个demo说明如何使用spring和quartz配置多个定时任务. 环境: eclipse + ...
- socket listen/accept
listen函数 摘要:listen函数使用主动连接套接口变为被连接套接口,使得一个进程可以接受其它进程的请求,从而成为一个服务器进程.在TCP服务器编程中listen函数把进程变为一个服务器,并指定 ...
- Linux - Ubuntu Server基础
Ubuntu Server:部署环境,用来部署项目的server系统. XShell:用来连接linux的工具.web项目要部署到远程服务器上,所以需要XShell来连接远程服务器. pycharm: ...
- poj2420(模拟退火大法好)
// // main.cpp // poj2420 // // Created by 陈加寿 on 16/2/13. // Copyright © 2016年 chenhuan001. All rig ...
- SharePoint服务器端对象模型 之 对象模型概述(Part 2)
(三)Url 作为一个B/S体系,在SharePoint的属性.方法参数和返回值中,大量的涉及到了Url,总的来说,涉及到的Url可以分为如下四类: 绝对路径:完整的Url,包含了协议头(http或h ...
- :eq()中的参数详解——需要注意
:eq()的括号中的参数如果是非负数则在DOM对象中按从上到下的顺序查找指定的元素,但是,如果是是负数,则在DOM中从下向上查找指定元素. 如下实例: 如果 $("#divTag div:e ...
- an open source web server and reverse proxy
https://www.nginx.com/resources/admin-guide/ NGINX is an open source web server and reverse proxy th ...