CLLocation
http://blog.sina.com.cn/s/blog_9e8867eb01013knc.html 这家伙写的不错本人也参考了这篇博客,希望原文博主可以谅解新手的无奈举措
首相要提到的类是 CLLocationManager 这个类 英文描述如下
The CLLocationManager class is the central point for configuring the delivery of location- and heading-related events to your app. You use an instance of this class to establish the parameters that determine when location and heading events should be delivered and to start and stop the actual delivery of those events. You can also use a location manager object to retrieve the most recent location and heading data.
CLLocationManager 类是配置到您的应用程序的位置和标题相关的事件交互的关键。您可以使用此类的实例建立的参数的确定位置和事件的标题在交互时,启动和停止这些事件的实交 互。也可以使用一个位置管理器对象来检索标题数据与最近的位置。简单的说就是可以实现定位管理的功能
定位的3种方式:
1.GPS ,最精确的定位方式 (原博主说iPhone1不支持)
2.蜂窝三角定位,(应该是利用数据来定位的,没有涉猎到这部分知识,回头补上:DOTO? 依靠基站比较密集的城市会比较准确)
3.WiFi,
使用如下:
首先要有一个CLLocationManager这个类作为属性 来管理定位
要使用CLLocationManager这个类就必须遵守这个类的协议
遵守协议实现如下的方法:
1.监控用户是否同意开启定位功能(此操作应该是iOS8以后才有的,后面当然也有iOS8之前的定位操作)
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status;
参数status是一个枚举,如下:
kCLAuthorizationStatusNotDetermined 不确定,不知道什么时候是使用
kCLAuthorizationStatusRestricted 有限制的使用
kCLAuthorizationStatusDenied 不使用
kCLAuthorizationStatusAuthorizedAlways 总是使用
kCLAuthorizationStatusAuthorizedWhenInUse 在使用的情况下开启
kCLAuthorizationStatusAuthorized 自动开启
2.监听已经定位到得用户的位置(前提是已经开启定位服务)
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations;
参数locations 是一个数组 记录了位置的变化信息,其中数组中的最后一个对象是最新的位置信息,也就是目前的位置信息
由方法可以知道数组中的元素的类型是CLLocation类型的 可以的得到其中的元素,CLLocation类型的对象有一个坐标属性叫做(coordinate)类型是CLLocationCoordinate2D 是一个结构体 其中包含了精度和纬度(double型)
CLLocationDegrees latitude;维度
CLLocationDegrees longitude; 经度
使用方式
引入CoreLocation文件包 ,需要手动导入
需要CLLocationManager来开启定位服务,定位服务是轮询的,为对程序来说是需要一定时间才会得到的(表示不懂什么意思),所以CLLocationManager的操作都交给代理来完成,设置当前的控制器为代理
4.操作步骤
创建位置管理Manager对象
self.locationManager = [[CLLocationManager alloc] init];
将控制器设置为代理
locationManager.delegate=self;
//为设置定位的精度,可以设为最优,装置会自动用最精确的方式去定位
self.mgr.desiredAccuracy=kCLLocationAccuracyBest;
//distanceFilter是距离过滤器,为了减少对定位装置的轮询次数,位置的改变不会每次都去通知委托,而是在移动了足够的距离时才通知委托程序,它的单位是米
self.mgr.distanceFilter=100.0f;
下面来判断当前的iOS版本,来选择不同的定位方式
#define kIOSVersion [[UIDevice currentDevice].systemVersion doubleValue]
if (kIOSVersion>8.0) {
//选择使用方式 +征求意见(info.plist)
如果是大于iOS8的,就需要手动设置plist文件
苹果文档是这么说的:
* If the NSLocationWhenInUseUsageDescription key is not specified in your
* Info.plist, this method will do nothing, as your app will be assumed not
* to support WhenInUse authorization.
*/
// - (void)requestWhenInUseAuthorization
[self.mgr requestWhenInUseAuthorization];
}else{
//本版低于iOS8
[self.mgr startUpdatingLocation ];
}
//监听用户的操作:是否同意定位
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{
switch (status) {
//拒绝
case kCLAuthorizationStatusDenied:
NSLog(@"用户不允许定位");
break;
case kCLAuthorizationStatusAuthorizedWhenInUse:
[ self.mgr startUpdatingLocation];
break;
default:
break;
}
}
//监听已经定位到得用户的位置(用户位置已知,说明已经定位到了)(不会的方法直接option键查看)
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
//注意1:返回的位置是数组 (最后一项是最新的位置)
CLLocation *latestLocation=locations.lastObject;
//注意2:CLLocation中coordinate(坐标位置)
CLLocationCoordinate2D coordinate=latestLocation.coordinate;
NSInteger latitude=coordinate.latitude;
NSInteger longitude=coordinate.longitude;
NSLog(@"latitude:%ld longitude %ld",(long)latitude,(long)longitude);
//如果只定位1次
self.mgr=nil;
//结束定位
[self.mgr stopUpdatingLocation];
}
CLLocation的更多相关文章
- iOS地图 -- 定位中的CLLocation的介绍与小练习
通过定位练习,熟悉CLLocation 在上篇笔记中提到了CLLocation类,这里通过练习来讲解一下这个类,类中包含了获取到的用户位置的信息 coordinate --> 坐标,经度和纬度 ...
- 获取经纬度 CLLocation
//导入库 #import <CoreLocation/CoreLocation.h> //注意: //需要在 info.plist 中导入前两个字段 //NSLocationAlways ...
- CLLocation的属性以及使用的解释
http://blog.csdn.net/u012496940/article/details/47405345 上一篇的链接(一个定位实例) 从上一篇中的实例了解所使用的一些元素: CLLcati ...
- iOS 原生地图地理编码与反地理编码
当我们要在App实现功能:输入地名,编码为经纬度,实现导航功能. 那么,我需要用到原生地图中的地理编码功能,而在Core Location中主要包含了定位.地理编码(包括反编码)功能. 在文件中导入 ...
- iOS开发系列--地图与定位
概览 现在很多社交.电商.团购应用都引入了地图和定位功能,似乎地图功能不再是地图应用和导航应用所特有的.的确,有了地图和定位功能确实让我们的生活更加丰富多彩,极大的改变了我们的生活方式.例如你到了一个 ...
- iOS之获取经纬度并通过反向地理编码获取详细地址
_locationManager = [[CLLocationManager alloc] init]; //期望的经度 _locationManager.desiredAccuracy = kCLL ...
- IOS开发基础知识碎片-导航
1:IOS开发基础知识--碎片1 a:NSString与NSInteger的互换 b:Objective-c中集合里面不能存放基础类型,比如int string float等,只能把它们转化成对象才可 ...
- iOS项目iCloud及CloudKit Dashboard运用
CloudKit是苹果推出的基于iCloud的一个云端数据存储服务.其 主要由下面两部分组成: 一个仪表web页面,用于管理公开数据的记录类型. 一组API接口,用于iCloud和设备之间的数据传递. ...
- 定位框一闪而过 iOS Swift
需求:获取经纬度. 方案:我自定义了一个类模块CLLocationModule.swift 备注以下代码里 let IS_IOS8 = (UIDevice.currentDevice().system ...
随机推荐
- Java DES 加密和解密源码(转)
原文地址:http://www.oschina.net/code/snippet_727646_18383 Java密码学结构设计遵循两个原则: 1) 算法的独立性和可靠性. 2) 实现的独立性和相互 ...
- 刚子扯谈:源于Chanel的图片描述
文/刚子 2013年8月9日 北京晴 猴晒猴晒 开片语:真心不知道今天该分享点啥?先扯几句牢骚,我个人认为对朋友也够意思,虽然他们有的时候挺操蛋的,虽然还简称哥们儿,虽然还一起交流,但已经变成了无谓的 ...
- Android UI ActionBar功能-自动隐藏 Action Bar
为了使ActionBar不影响Activity的布局内容,我们还可以设置ActionBar,将其设置为透明,并且让Activity是头部自动空出一个ActionBar的空间: 官方文档:http:// ...
- 排颜色问题——数组 leetcode lintcode
问题描写叙述: 给一个数组,而且数组里面元素的值仅仅可能是0,1,2,然后如今把这个数组排序. 第二种表述: 现有n个红白蓝三种不同颜色的小球,乱序排列在一起,请通过两两交换随意两个球,使得从左至右, ...
- 《think in python》学习-6
think in python 有返回函数 我们使用过的内置函数中,有一部分会返回结果,比如 math的 返回值 我们写一个有返回值的函数,计算给定半径的圆的面积,例如这个: def area(rad ...
- SqlServer导库语句
GO /****** 对象: StoredProcedure [dbo].[sp_ExportDatabase] 脚本日期: 07/18/2013 12:37:26 ******/ SET ANSI_ ...
- 自定义配置文件的使用(web.config/app.config)
以下非原创作品,但都是自己看过理解并写过,记录下来,以便之后项目的使用或其它用途. (1)只需要简单配置单一属性值: <configuration> <configSections& ...
- 网络分析shell脚本(实时流量+连接统计)
介绍一个强大的分析网络的shell脚本,此脚本是从EZHTTP拆分出来的,觉得有必要单独介绍下.脚本运行效果截图: 此脚本包含的功能有: 1.实时监控任意网卡的流量 2.统计10秒内平均流量 3.统计 ...
- 底层由于接收到操作系统的信号而停止(the inferior stopped because it triggered an exception)
QT开发内存管理问题: 在linux上提示:底层由于接收到操作系统的信号而停止: 在windows上提示:the inferior stopped because it triggered an ex ...
- 如果不知道MySQL当前使用配置文件(my.cnf)的路径的解决方法
如果不知道当前使用的配置文件的路径,可以尝试下面的操作: # which mysqld /usr/local/mysql/bin/mysqld # /usr/local/mysql/bin/mysql ...