感谢源文章作者的总结:
http://blog.csdn.net/xiaoxiaobukuang/article/details/51726904
CoreLocation框架(CoreLocation.framework)可用于定位设备当前的经纬度,通过该框架,应用程序可通过附近的蜂窝基站、WIFI信号或者GPS等信息计算用户位置。
iOS SDK提供了CLLocationManager、CLLocationManagerDelegate来处理设备的定位信息,包括获取设备的方向以及进行方向检测等。其中CLLocationManager是整个CoreLocation框架的核心,定位、方向检测、区域检测等都由该API完成;而CLLocationManagerDelegate是一个协议,实现该协议的对象可作为CLLocationManager的delegate对象,负责处理CLLocationManager的定位、方向检测、区域检测等相关事件。除此之外,iOS还提供了CLLocation(代表位置信息)、CLLHeading(代表设备方向)、CIRegion(代表区域)等API,这些API与CLLocationManager、CLLocationManagerDelegate共同组成了iOS的CoreLocation框架。
GPS是英文:Global Positioning System(全球定位系统)的简称;该定位系统由3部分组成,即由GPS卫星组成的空间部分、若干地面站的控制部分和普通用户手中的接收机这3部分。
与基站、WIFI定位相比,GPS定位耗电量最大,速度最慢,但是精度最高。
每个手机基站都有一个标识符,iOS设备可以搜集周围所有收到信号的基站和它们的标识符,通过联网发送到苹果云端服务器,再由服务器根据这些基站的位置信息查询并计算出当前位置,然后把该定位信息返回到手机。
iOS设备通过无线卡搜集周围所有的WIFI热点,获得它们的MAC地址,然后通过Apple的云端服务器查询该WiFi热点是否已经登记,如果已经登记,即可获取该WiFi热点的位置,最后通过对多个WiFi热点折中计算得到当前位置并返给iOS设备。
iOS开发者使用CoreLocation.framework框架进行定位非常简单,CoreLocation框架的常用API主要有如下几个:
(1)、CLLocationManager:定位管理器类;(2)、CLLocationManagerDelegate:该协议代表定位管理器的delegate协议。实现该协议的对象可负责处理CLLocationManager的定位事件;(3)、CLLocation:该对象代表位置。该对象包含了当前设备的经度、纬度、高度、速度、路线等信息,还包含了该定位信息的水平精确度、垂直精确度以及时间戳信息;(4)、CLHeading:该对象代表设备的移动方向;(5)、CLRegion:该对象代表一个区域。一般程序不会直接使用该类,而是使用它的两个子类,即CLCircularRegion(圆形区域)和CLBeaconRegion(蓝牙信号区);除此之外,CoreLocation框架还涉及一个CLLocationCoordinate2D结构体变量,该几诶够提变量包含经度、纬度两个值,其中CLLocation对象的coordinate属性就是一个CLLocationCoordinate2D结构体变量。
提示:为了在iOS应用中使用CoreLocation.framework,需要完成两件事情: ①、为应用添加CoreLocation.framework框架; ②、在需要使用定位服务及相关类的源文件重使用#import < CoreLocation/CoreLocation.h >导入CoreLocation.framework的头文件。
CoreLocationManager提供了如下类方法判断当前设备的定位相关服务状态:
<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;">(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>)、 + (<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">BOOL</span>)locationServicesEnabled __OSX_AVAILABLE_STARTING(__MAC_10_7,__IPHONE_4_0);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//返回当前定位服务是否可用;</span> (<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>)、 + (<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">BOOL</span>)deferredLocationUpdatesAvailable __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_6_0);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//返回延迟定位更新是否可用;</span> (<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>)、 + (<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">BOOL</span>)significantLocationChangeMonitoringAvailable __OSX_AVAILABLE_STARTING(__MAC_10_7,__IPHONE_4_0);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//返回重大位置监听是否可用;</span> (<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4</span>)、 + (<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">BOOL</span>)headingAvailable __OSX_AVAILABLE_STARTING(__MAC_10_7,__IPHONE_4_0);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//返回该设备是否支持磁力计计算方向;</span> (<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span>)、 + (<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">BOOL</span>)isRangingAvailable __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_7_0);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//返回蓝牙信号范围是否可用。</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li></ul>除此之外,在使用CLLocationManager开始定位之前,还可为该对象设备如下属性:
(1)、 @property(assign, nonatomic) BOOL pausesLocationUpdatesAutomatically __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_6_0);设置iOS设备是否可暂停定位来节省电池的电量;如果该属性设为『YES』,则当iOS设备不再需要定位数据时,iOS设备可以自动暂停定位;(2)、 @property(assign, nonatomic) CLLocationDistance distanceFilter;设置CLLocationManager的自动过滤距离。也就是说,只有当设备在水平方向的位置改变超过该数值(以米为单位)指定的距离时才会生成一次位置变化的信号;(3)、 @property(assign, nonatomic) CLLocationAccuracy desiredAccuracy;设置定位服务的静度。该属性值支持 <code class="hljs cs has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">extern</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">const</span> CLLocationAccuracy kCLLocationAccuracyBestForNavigation __OSX_AVAILABLE_STARTING(__MAC_10_7,__IPHONE_4_0);(导航级的最佳精确度); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">extern</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">const</span> CLLocationAccuracy kCLLocationAccuracyBest;(最佳精确度); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">extern</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">const</span> CLLocationAccuracy kCLLocationAccuracyNearestTenMeters;(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span>米误差); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">extern</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">const</span> CLLocationAccuracy kCLLocationAccuracyHundredMeters;(百米误差); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">extern</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">const</span> CLLocationAccuracy kCLLocationAccuracyKilometer;(千米误差); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">extern</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">const</span> CLLocationAccuracy kCLLocationAccuracyThreeKilometers;(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>三千米误差)</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li></ul>等常量值。当然,也可直接指定一个浮点数作为定位服务允许的误差;
(4)、 @property(assign, nonatomic) CLActivityType activityType __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_6_0);设置定位数据的用途。该属性支持 <code class="hljs fix has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-attribute" style="box-sizing: border-box;">{ CLActivityTypeOther </span>=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;"> 1,(定位数据作为普通用途) CLActivityTypeAutomotiveNavigation,(定位数据作为车辆导航使用) CLActivityTypeFitness,(定位数据作为步行导航使用) CLActivityTypeOtherNavigation(定位数据作为其他导航使用) };</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li></ul>注意:iOS8中使用CoreLocation定位
(1)、在使用CoreLocation需要调用如下函数 iOS8对定位进行了一些修改,其中包括定位授权的方法,CLLocationManager增加了如下两个方法:①、始终允许访问位置信息
<code class="hljs erlang has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-pp" style="box-sizing: border-box;">- <span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">(void)</span>requestAlwaysAuthorization __OSX_AVAILABLE_STARTING<span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">(<span class="hljs-variable" style="box-sizing: border-box;">_</span>_<span class="hljs-variable" style="box-sizing: border-box;">MAC_NA</span>, <span class="hljs-variable" style="box-sizing: border-box;">_</span>_<span class="hljs-variable" style="box-sizing: border-box;">IPHONE_8_0</span>)</span>;</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>②、使用应用程序期间允许方位位置数据
<code class="hljs erlang has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-pp" style="box-sizing: border-box;">- <span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">(void)</span>requestWhenInUseAuthorization __OSX_AVAILABLE_STARTING<span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">(<span class="hljs-variable" style="box-sizing: border-box;">_</span>_<span class="hljs-variable" style="box-sizing: border-box;">MAC_NA</span>, <span class="hljs-variable" style="box-sizing: border-box;">_</span>_<span class="hljs-variable" style="box-sizing: border-box;">IPHONE_8_0</span>)</span>; </span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul> (2)、在Info.plist文件中添加如下配置:①、NSLocationAlwaysUsageDescription
②、NSLocationWhenInUseUsageDescription
这两个键的值就是授权alert的描述,实例配置如下: Demo如下:
<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;">- (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span>)clickBtn{ <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//如果定位服务可用</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> ([CLLocationManager locationServicesEnabled]) { <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"开始执行定位服务"</span>); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//设置定位精度:最佳精度</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.locationManager</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.desiredAccuracy</span> = kCLLocationAccuracyBest; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//设置距离过滤器为50米,表示每移动50米更新一次位置</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.locationManager</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.distanceFilter</span> = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">50</span>; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//将试图控制器自身设置为CLLocationManager的delegate</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//因此该试图控制器需要实现CLLocationManagerDelegate协议</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.locationManager</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.delegate</span> = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span>; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> ([[[UIDevice currentDevice] systemVersion] floatValue] >= <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">8.0</span>) { [<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.locationManager</span> requestWhenInUseAuthorization]; [<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.locationManager</span> requestAlwaysAuthorization]; } <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//开始监听定位信息</span> [<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.locationManager</span> startUpdatingLocation]; }<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">else</span>{ <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"无法使用定位服务!!!"</span>); } } <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#pragma mark -- CLLocationManagerDelegate</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//成功获取定位数据后将会激发该方法</span> - (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span>)locationManager:(CLLocationManager *)manager didUpdateLocations:(<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSArray</span> *)locations{ <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//获取最后一个定位数据</span> CLLocation* location = [locations lastObject]; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//依次获取CLLocation中封装的经度、纬度、高度、速度、方向等信息</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"经度:%g,纬度:%g,高度:%g,速度:%g,方向:%g"</span>,location<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.coordinate</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.latitude</span>,location<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.coordinate</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.longitude</span>,location<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.altitude</span>,location<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.speed</span>,location<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.course</span>); } <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//定位失败时激发的方法</span> - (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span>)locationManager:(CLLocationManager *)manager didFailWithError:(<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSError</span> *)error{ <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"定位失败:%@"</span>,error); }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li><li style="box-sizing: border-box; padding: 0px 5px;">32</li><li style="box-sizing: border-box; padding: 0px 5px;">33</li><li style="box-sizing: border-box; padding: 0px 5px;">34</li></ul>CLLocation对象包含如下属性,这些属性包含了定位相关信息:
<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;">(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>)、 <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">@property</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">readonly</span>, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">nonatomic</span>) CLLocationDistance altitude;<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//该属性表示当前设备的海拔高度,单位是米;</span> (<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>)、 <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">@property</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">readonly</span>, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">nonatomic</span>) CLLocationCoordinate2D coordinate;<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//该属性返回一个CLLocationCoordinate2D结构体变量,该结构体变量中包含经度、纬度信息;</span> (<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>)、 <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">@property</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">readonly</span>, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">nonatomic</span>) CLLocationDirection course __OSX_AVAILABLE_STARTING(__MAC_10_7,__IPHONE_2_2);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//该属性表示当前设备前进的方向。该值为0°表示向北,90°表示向东,180°表示向南,270°表示向西;</span> (<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4</span>)、 <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">@property</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">readonly</span>, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">nonatomic</span>) CLLocationAccuracy horizontalAccuracy;<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//该属性表明定位信息的水平精确度。将返回的坐标作为圆心,并将水平精确度视为半径。真正的设备位置落在此圆内的某处。此圆越小,位置就越精确;此圆越大,则位置越不精确。如果精确度为负值,则表明测量精确度失败;</span> (<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span>)、 <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">@property</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">readonly</span>, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">nonatomic</span>) CLLocationAccuracy verticalAccuracy;<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//该属性标明定位信息的垂直精确度。也就是说,iOS设备的实际高度在该定位信息的高度加或减该属性值的范围内;</span> (<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">6</span>)、 <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">@property</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">readonly</span>, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">nonatomic</span>, copy) <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSDate</span> *timestamp;<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//该属性返回定位信息的返回时间;</span> (<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">7</span>)、 <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">@property</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">readonly</span>, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">nonatomic</span>) CLLocationSpeed speed __OSX_AVAILABLE_STARTING(__MAC_10_7,__IPHONE_2_2);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//该属性表示返回设备的移动速度,单位是米/秒。实际上,该属性适用于行车减速,而不太适用于步行速度。</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li></ul>①、自定位置:开发者可以自行输入位置的经度值、纬度值;②、City Bicycle Ride:模拟设备携带者在城市中骑车移动;③、City Run:模拟设备携带者在城市中跑步;④、Freeway Drive:模拟设备携带者再告诉公路中驾车;
如果开发者希望计算平均移动速度,则只要不断的累计设备的移动距离和移动时间,再用距离除以时间即可得到设备的平均移动速度。
例如:
<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#pragma mark -- CLLocationManagerDelegate</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//成功获取定位数据后将会激发该方法</span> - (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span>)locationManager:(CLLocationManager *)manager didUpdateLocations:(<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSArray</span> *)locations{ <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//获取最后一个定位数据</span> CLLocation* newLocation = [locations lastObject]; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (newLocation<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.horizontalAccuracy</span> < kCLLocationAccuracyHundredMeters) { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.prevLocation</span>) { <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//计算本次定位数据与上次定位数据之间的时间差</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSTimeInterval</span> dTime = [newLocation<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.timestamp</span> timeIntervalSinceDate:<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.prevLocation</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.timestamp</span>]; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//累计行车时间</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.sumTime</span> += dTime; <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">CGFloat</span> distance = [newLocation distanceFromLocation:<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.prevLocation</span>]; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//如果距离小于1米。则忽略本次数据,直接返回该方法</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (distance < <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1.</span>f) { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span>; } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.sumDistance</span> += distance; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//计算移动速度,将米/秒换算成千米/小时,需要乘以3.6</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">CGFloat</span> speed = distance/dTime*<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3.6</span>; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//计算平均速度</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">CGFloat</span> avgSpeed = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.sumDistance</span>/<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.sumTime</span>*<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3.6</span>; <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span>* speedFeedback = [<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> stringWithFormat:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"当前速度为:%g千米/小时,平均速度为:%g千米/小时。合计移动:%g前面"</span>,speed,avgSpeed,<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.sumDistance</span>/<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1000</span>]; <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"%@"</span>,speedFeedback); } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.prevLocation</span> = newLocation; } }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li></ul>拥有GPS硬件的设备可以生成设备的当前方向(couse属性)和速度信息。iPhone设备携带的定位管理器可以返回一个已经计算好的course值,通过这个值我们可以获得当前前进的方向,course值是0~360之间的浮点数,0°值表示正北方向,90°值表示正东方向,180°值表示正南方向,270°值表示正西方向,程序可以通过course值来检测用户位置的移动方向。
使用CLLocationManager获取设备方向的步骤如下:
①、创建CLLocationManager对象,该对象负责获取定位相关信息。并为该对象设备一些必要的属性;②、为CLLocationManager指定delegate属性,该属性必须是一个实现CLLocationManagerDelegate协议的对象。实现CLLocationManagerDelegate协议时刻根据需要实现协议中特定的方法。③、调用CLLocationManager的starUpdatingHeading方法获取方向信息。获取方向结束时,可调用stopUpdatingHeading方法结束获取方向信息。当设备的方向改变时,iOS系统将会自动激发CLLocationManager的delegate对象的
<code class="hljs erlang has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-pp" style="box-sizing: border-box;">- <span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">(void)</span>locationManager:<span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">(<span class="hljs-variable" style="box-sizing: border-box;">CLLocationManager</span> *)</span>manager didUpdateHeading:<span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">(<span class="hljs-variable" style="box-sizing: border-box;">CLHeading</span> *)</span>newHeading __OSX_AVAILABLE_STARTING<span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">(<span class="hljs-variable" style="box-sizing: border-box;">_</span>_<span class="hljs-variable" style="box-sizing: border-box;">MAC_NA</span>,<span class="hljs-variable" style="box-sizing: border-box;">_</span>_<span class="hljs-variable" style="box-sizing: border-box;">IPHONE_3_0</span>)</span>;</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>方法,而程序可通过重写该方法来获取设备方向;
iOS允许为检测方向改变设置如下属性:
①、CLLocationDegrees headingFilter:设置只有当设置方向的改变值超过该属性值时才激发delegate方法;②、CLDeviceOrientation headingOrientation:设置设备当前方向;监听方向时返回的是一个CLHeading对象,该对象包含如下属性:
<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;">(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>)、 <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">@property</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">readonly</span>, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">nonatomic</span>) CLLocationDirection magneticHeading;<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//该属性返回设备与磁北的相对方向;</span> (<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>)、 <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">@property</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">readonly</span>, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">nonatomic</span>) CLLocationDirection trueHeading;<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//该属性返回设备与真北的相对方向;</span> (<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>)、 <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">@property</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">readonly</span>, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">nonatomic</span>) CLLocationDirection headingAccuracy;<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//该属性返回方向值的错误范围;</span> (<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4</span>)、 <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">@property</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">readonly</span>, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">nonatomic</span>, copy) <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSDate</span> *timestamp;<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//该属性返回方向值的生成时间;</span> (<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span>)、 <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">@property</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">readonly</span>, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">nonatomic</span>) CLHeadingComponentValue x;<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//获取该设备在X方向上监听得到的原始磁力值,该磁力值的强度单位是微特斯拉;</span> (<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">6</span>)、 <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">@property</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">readonly</span>, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">nonatomic</span>) CLHeadingComponentValue y;<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//获取该设备在Y方向上监听得到的原始磁力值,该磁力值的强度单位是微特斯拉;</span> (<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">7</span>)、 <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">@property</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">readonly</span>, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">nonatomic</span>) CLHeadingComponentValue z;<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//获取该设备在Z方向上监听得到的原始磁力值,该磁力值的强度单位是微特斯拉;</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li></ul>如果希望iOS设备进入某个区域发出通知,那么这种区域监测的功被称为临近警告;用户设备不断地临近固定点,当与该固定点的距离小于指定范围时,系统可以触发相应的处理。用户设备离开指定固定点,当与该固定点的距离大于指定范围时,系统也可以出发相应的处理。
iOS的区域监测同样可以使用CLLocationManager来实现,监听设备是否进入/离开某个区域的步骤如下:
①、创建CLLocationManager对象,该对象负责获取定位相关信息,并为该对象设置一些必要的属性。对于区域监测而言,CLLocationManager对象需要设置monitoredRegions属性,该属性值用于设置该设备监听的多个区域;②、为CLLocationManager指定delegate属性,该属性值必须是一个实现CLLocationManagerDelegate协议的对象。实现 CLLocationManagerDelegate协议时刻根据需要实现协议中特定的方法;③、调用CLLocationManager的startMonitoringForRegion:方法进行区域监测,区域监测结束时,可调用stopMonitoringForRegion:方法结束区域监测。