天天看點

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軸方向值

方向定位時間戳

繼續閱讀