使用CoreLocation定位

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框架。

一、iOS的定位支持

1、GPS卫星定位

GPS是英文:Global Positioning System(全球定位系统)的简称;该定位系统由3部分组成,即由GPS卫星组成的空间部分、若干地面站的控制部分和普通用户手中的接收机这3部分。

与基站、WIFI定位相比,GPS定位耗电量最大,速度最慢,但是精度最高。

2、基站定位

每个手机基站都有一个标识符,iOS设备可以搜集周围所有收到信号的基站和它们的标识符,通过联网发送到苹果云端服务器,再由服务器根据这些基站的位置信息查询并计算出当前位置,然后把该定位信息返回到手机。

3、WIFI定位

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结构体变量。

1、获取位置信息

 使用CoreLocation.framework进行定位如下步骤:
  • ①、创建CLLocationManager对象,该对象负责获取定位相关信息。并为该对象设置一些必要的属性;
  • ②、为CLLocationManager指定delegate属性,该属性必须是一个实现CLLocationManagerDelegate协议的对象。实现CLLocationManagerDelegate协议时刻根据需要实现协议中特定的方法;
  • ③、调用CLLocationManager的startUpdatingLocation方法获取定位信息。定位结束时,可调用stopUpdatingLocation方法结束获取定位信息。

提示:为了在iOS应用中使用CoreLocation.framework,需要完成两件事情: 
①、为应用添加CoreLocation.framework框架; 
②、在需要使用定位服务及相关类的源文件重使用#import < CoreLocation/CoreLocation.h >导入CoreLocation.framework的头文件。

CoreLocationManager提供了如下类方法判断当前设备的定位相关服务状态:

<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">(<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; 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; background-color: rgb(238, 238, 238);"><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><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; 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; background-color: rgb(238, 238, 238);"><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; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><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; 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; background-color: rgb(238, 238, 238);"><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><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; 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; background-color: rgb(238, 238, 238);"><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; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><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; 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; background-color: rgb(238, 238, 238);"><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><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; 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; background-color: rgb(238, 238, 238);"><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; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><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; 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; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; 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; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

②、使用应用程序期间允许方位位置数据

<code class="hljs erlang has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><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="bo
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值