CoreLocation基本使用

2021-07-09 14:01:23 字數 3013 閱讀 5350

匯入框架,匯入標頭檔案,引入全域性cllocationmanager物件(因為整個工程都要用到它,不能讓他死);

//設定**

self.manager.delegate = self;

// 設定多久獲取一次

self.manager.distancefilter = 500;

// 設定獲取位置的精確度

self.manager.desiredaccuracy = kcllocationaccuracynearesttenmeters;

/*

注意: ios7只要開始定位, 系統就會自動要求使用者對你的應用程式授權. 但是從ios8開始, 想要定位必須先"自己""主動"要求使用者授權

在ios8中不僅僅要主動請求授權, 而且必須再info.plist檔案中配置一項屬性才能彈出授權視窗

nslocationwheninusedescription,允許在前台獲取gps的描述

nslocationalwaysusagedescription,允許在後台獲取gps的描述

*/if ([[uidevice currentdevice].systemversion doublevalue] >= 8.0)

else

#pragma mark - cllocationmanagerdelegate

/** * 授權狀態發生改變時呼叫

status:當前授權的狀態

ios8以下和ios8以上都可以用,但是ios8以下的時候可能有時候不需要,但是當》=ios8的時候就需要這個方法了,判斷狀態後開始定位

*/- (void)locationmanager:(cllocationmanager *)manager didchangeauthorizationstatus:(clauthorizationstatus)status

else

if (status == kclauthorizationstatusauthorizedalways || status == kclauthorizationstatusauthorizedwheninuse)

else

}/**

* 獲取到位置資訊之後就會呼叫(呼叫頻率非常高)

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

2.做汽車導航用到以下兩點

//計算兩個location之間的距離:distancefromlocation

cllocationdistance distance = [newlocation distancefromlocation:self.previouslocation];

// 計算兩次之間的時間(單位只秒)

nstimeinterval dtime = [newlocation.timestamp timeintervalsincedate:self

.previouslocation

.timestamp];

3.在做指南針的時候,獲取的不是使用者的位置,而是使用者的方向,呼叫的是如下**,注意一點,獲取使用者方向是不需要使用者授權的,所以不需要判斷是否大於ios8

// 3.開始獲取使用者位置

// 注意:獲取使用者的方向資訊是不需要使用者授權的

[self.manager startupdatingheading];

#pragma mark - cllocationmanagerdelegate

// 當獲取到使用者方向時就會呼叫

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

4.區域檢測

在某乙個圓形區域中,要是進入或者離開這個區域則會呼叫相應的**方法

// 2.成為corelocation管理者的**監聽獲取到的位置

self.mgr.delegate = self;

// 注意:如果是ios8, 想進行區域檢測, 必須自己主動請求獲取使用者隱私的許可權(因為是要知道使用者的位址的呀)

if ([[uidevice currentdevice].systemversion doublevalue] >= 8.0 )

// 3.開始檢測使用者所在的區域

// 3.1建立區域

// clregion 有兩個子類是專門用於指定區域的

// 乙個可以指定藍芽的範圍/ 乙個是可以指定圓形的範圍

// 建立中心點

cllocationcoordinate2d center = cllocationcoordinate2dmake(40.058501, 116.304171);

// c建立圓形區域, 指定區域中心點的經緯度, 以及半徑

clcircularregion *circular = [[clcircularregion alloc] initwithcenter:center radius:500 identifier:@"軟體園"];

[self.mgr startmonitoringforregion:circular];

#pragma mark - cllocationmanagerdelegate

// 進入監聽區域時呼叫

- (void)locationmanager:(cllocationmanager *)manager didenterregion:(clregion *)region

// 離開監聽區域時呼叫

- (void)locationmanager:(cllocationmanager *)manager didexitregion:(clregion *)region

Javascript cssText基本使用指北

一 csstext之起步 那些年,我們是這樣設定樣式的 style.width 233px style.position fixed style.left 233px 現如今,我們可以這樣搞 style.csstext width 233px height 233px position fixed ...

ios NSUserDefaults基本使用

nsuserdefaults適合儲存輕量級的本地資料儲存,比如儲存登陸介面的使用者名稱 密碼之類的資料,並使之本地持久化。nsuserdefaults支援的資料格式有 nsnumber integer float double nsstring,nsdate,nsarray,nsdictionary...

swift UIBezierPath基本使用

參考 import uikit uibezierpath 使用 instancetype bezierpath 可以根據我們的需要任意定製樣式 instancetype bezierpathwithrect cgrect rect 根據乙個矩形畫貝塞爾曲線 instancetype bezierpa...