天天看点

IOS定位服务的应用

在ios8之后,ios的定位服务做了优化,若要使用定位服务,必须先获取用户的授权。

首先需要在info.plist文件中添加一个键:nslocationalwaysusagedescription或者nslocationwheninuseusagedescription。其中nslocationalwaysusagedescription是要始终使用定位服务,nslocationwheninuseusagedescription是只在前台使用定位服务。

IOS定位服务的应用

ios8中cllocationmanager新增的两个新方法:

- (void)requestalwaysauthorization;

- (void)requestwheninuseauthorization;

这两个方法对应上面的两个键值,用于在代码中申请定位服务权限。

ios的定位服务在corelocation.framework框架内,首先引入这个框架:

IOS定位服务的应用

开启定位服务的代码非常简单,示例如下:

<a href="http://my.oschina.net/u/2340880/blog/414760#">?</a>

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

<code>#import "viewcontroller.h"</code>

<code>#import &lt;corelocation/corelocation.h&gt;</code>

<code>@interface viewcontroller ()&lt;cllocationmanagerdelegate&gt;</code><code>//定位服务的代理</code>

<code>@end</code>

<code>@implementation viewcontroller</code>

<code>- (</code><code>void</code><code>)viewdidload {</code>

<code>    </code><code>[super viewdidload];</code>

<code>    </code><code>cllocationmanager* manager = [[cllocationmanager alloc]init];</code><code>//初始化一个定位管理对象</code>

<code>    </code><code>[manager requestwheninuseauthorization];</code><code>//申请定位服务权限</code>

<code>    </code><code>manager.delegate=self;</code><code>//设置代理</code>

<code>    </code><code>[manager startupdatinglocation];</code><code>//开启定位服务</code>

<code>}</code>

<code>//定位位置改变后调用的函数</code>

<code>-(</code><code>void</code><code>)locationmanager:(cllocationmanager *)manager didupdatelocations:(nsarray *)locations{</code>

<code>    </code><code>nslog(@</code><code>"%@"</code><code>,locations);</code>

cllocationmanager相关方法解读:

+ (bool)locationservicesenabled;

判断设备是否支持定位服务

+ (bool)headingavailable;

判断设备是否支持航向信息功能(海拔,速度,方向等传感器的支持)

+ (bool)significantlocationchangemonitoringavailable;

判断设备是否支持更新位置信息

+ (bool)ismonitoringavailableforclass:(class)regionclass;

判断设备是否支持区域检测,regionclass是地图框架中的类。

+ (bool)israngingavailabl;

判断设备是否支持蓝牙测距

+ (clauthorizationstatus)authorizationstatus;

获得定位服务的授权状态,clauthorizationstatus的枚举如下:

<code>typedef</code> <code>ns_enum(</code><code>int</code><code>, clauthorizationstatus) {</code>

<code>    </code><code>kclauthorizationstatusnotdetermined = 0,</code><code>//用户还没有做选择</code>

<code>    </code><code>kclauthorizationstatusrestricted,</code><code>//应用拒接使用定位服务</code>

<code>    </code><code>kclauthorizationstatusdenied,</code><code>//用户拒绝授权</code>

<code>    </code><code>kclauthorizationstatusauthorizedalways,</code><code>//8.0后可用,始终授权位置服务</code>

<code>    </code><code>kclauthorizationstatusauthorizedwheninuse,</code><code>//8.0后可用,只在前台授权位置服务</code>

<code>};</code>

@property(assign, nonatomic) clactivitytype activitytype;

这个属性用来设置位置更新的模式,枚举如下:

<code>typedef</code> <code>ns_enum(nsinteger, clactivitytype) {</code>

<code>    </code><code>clactivitytypeother = 1,</code><code>//未知模式,默认为此</code>

<code>    </code><code>clactivitytypeautomotivenavigation,    </code><code>//车辆导航模式</code>

<code>    </code><code>clactivitytypefitness,                </code><code>//行人模式</code>

<code>    </code><code>clactivitytypeothernavigation         </code><code>//其他交通工具模式</code>

模式的应用可以起到节省电量的作用,例如车辆导航模式,当汽车停止时,位置更新服务会暂停。

@property(assign, nonatomic) cllocationdistance distancefilter;

设置位置更新的敏感范围,单位为米。

@property(assign, nonatomic) cllocationaccuracy desiredaccuracy;

设置定位服务的精确度,系统定义好的几个参数如下:

kcllocationaccuracybestfornavigation;//导航最高精确

kcllocationaccuracybest;//高精确

kcllocationaccuracynearesttenmeters;//10米

kcllocationaccuracyhundredmeters;//百米

kcllocationaccuracykilometer;//千米

kcllocationaccuracythreekilometers;//三公里

@property(assign, nonatomic) bool pauseslocationupdatesautomatically;

设置位置更新是否自动暂停

@property(readonly, nonatomic, copy) cllocation *location;

最后一次更新的位置信息,只读属性

@property(assign, nonatomic) cllocationdegrees headingfilter;

相关航向更新的敏感范围

@property(assign, nonatomic) cldeviceorientation headingorientation;

定位航向时的参照方向默认为正北,枚举如下:

<code>typedef</code> <code>ns_enum(</code><code>int</code><code>, cldeviceorientation) {</code>

<code>    </code><code>cldeviceorientationunknown = 0,</code><code>//方向未知</code>

<code>    </code><code>cldeviceorientationportrait,</code><code>//纵向模式</code>

<code>    </code><code>cldeviceorientationportraitupsidedown,</code><code>//纵向倒置模式</code>

<code>    </code><code>cldeviceorientationlandscapeleft,</code><code>//左向横向模式</code>

<code>    </code><code>cldeviceorientationlandscaperight,</code><code>//右向横向模式</code>

<code>    </code><code>cldeviceorientationfaceup,</code><code>//水平屏幕向上模式</code>

<code>    </code><code>cldeviceorientationfacedown</code><code>//水平屏幕下模式</code>

@property(readonly, nonatomic, copy) clheading *heading;

最后一个定位得到的航向信息

- (void)startupdatinglocation;

开启定位服务

- (void)stopupdatinglocation;

停止定位服务

- (void)startupdatingheading;

开启航向地理信息服务

- (void)stopupdatingheading;

停止航向地理信息服务

- (void)locationmanager:(cllocationmanager *)manager didupdatelocations:(nsarray *)locations;

位置更新后调用的方法,数组中是所有定位到的位置信息,最后一个是最新的。

- (void)locationmanager:(cllocationmanager *)manager didupdateheading:(clheading *)newheading;

航向信息更新后调用的方法

- (void)locationmanager:(cllocationmanager *)manager didfailwitherror:(nserror *)error;

定位异常时调用的方法

上面也提到,定位后返回的数组中存放的都是cllocation对象,这里面有很详细的位置信息,属性如下:

@property(readonly, nonatomic) cllocationcoordinate2d coordinate;

经纬度属性,cllocationcoordinate2d是一个结构体,如下:

<code>typedef</code> <code>struct</code> <code>{</code>

<code>    </code><code>cllocationdegrees latitude;</code><code>//纬度</code>

<code>    </code><code>cllocationdegrees longitude;</code><code>//经度</code>

<code>} cllocationcoordinate2d;</code>

@property(readonly, nonatomic) cllocationdistance altitude;

海拔高度,浮点型

@property(readonly, nonatomic) cllocationaccuracy horizontalaccuracy;

水平方向的容错半径

@property(readonly, nonatomic) cllocationaccuracy verticalaccuracy;

竖直方向的容错半径

@property(readonly, nonatomic) cllocationdirection course;

设备前进的方向,取值范围为0-359.9,相对正北方向

@property(readonly, nonatomic) cllocationspeed speed;

速度,单位为m/s

@property(readonly, nonatomic, copy) nsdate *timestamp;

定位时的时间戳

clheading对象的属性信息:

@property(readonly, nonatomic) cllocationdirection magneticheading;

设备朝向航标方向,0为北磁极。

@property(readonly, nonatomic) cllocationdirection trueheading;

设备朝向真实方向,0被地理上的北极

@property(readonly, nonatomic) cllocationdirection headingaccuracy;

方向偏差

@property(readonly, nonatomic) clheadingcomponentvalue x;

x轴的方向值

@property(readonly, nonatomic) clheadingcomponentvalue y;

y轴方向值

@property(readonly, nonatomic) clheadingcomponentvalue z;

z轴方向值

方向定位时间戳