使用Core Location獲取使用者的位置

2021-09-19 10:54:11 字數 3307 閱讀 9440

在使用core location之前,我們必須將corelocation.framework鏈結到xcode專案中。在需要使用core location的原始檔的中,匯入框架#import

有兩種服務可以獲取當前位置:

在某些情況下,定位服務可能不可用。比如:

因此,在嘗試啟用定位服務之前,應該呼叫cllocationmanager的類方法locationservicesenable。這個方法用來判斷系統的定位服務是否開發。如果返回no,當你嘗試開啟定位服務時,系統會提示使用者是否重新啟用定位服務。

接下來,給出一段**,按照上面的步驟啟用標準定位服務。

- (void)startstandardupdates 

self.locationmanager.delegate = self;

//設定`精度`和`觸發更新事件的最短水平距離`

self.locationmanager.desiredaccuracy = kcllocationaccuracybest;

self.locationmanager.distancefilter = 5;

//開啟標準定位服務

[self.locationmanager startupdatinglocation];

}

distancefilter屬性在更新位置之前裝置需要移動的最短水平距離,單位是公尺。上述**值為5,表示至少移動5公尺才會更新一次位置資訊。它的預設值是kcldistancefilternone,表示移動任何距離都會更新一次位置資訊,即使不移動依然會更新位置資訊。

啟用significant-change location service時,同樣需要建立cllocationmanager類的物件,然後給它的delegate賦值,呼叫startmonitoringsignificantlocationchanges方法就可以了。

- (void)startsignificantchangeupdates

無論使用哪一種定位服務,接收位置資料的方法都是一樣的。當位置資料可取時,location manager會呼叫locationmanager:didupdatelocations:**方法。在接收資料出錯時,location manager會呼叫locationmanager:didfailwitherror:**方法。

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

}

cllocation物件除了時間戳之外,還包括

這兩項是需要新增到info.plist中的。如果info.plist中沒有這兩項存在,即使呼叫requestalwaysauthorizationrequestwheninuseauthorization方法請求啟動定位服務,系統也會忽略請求。呼叫requestalwaysauthorization需要info.plist中有nslocationalwaysusagedescription,呼叫requestwheninuseauthorization需要info.plist中有nslocationwheninuseusagedescription

這兩項只有在ios 8.0之後的裝置上會起作用,在ios 6.0到ios 8.0的裝置上,使用nslocationusagedescription來請求啟用定位服務。

一般定位系統所返回的位置資訊都是經緯度,將經緯度逆向解析就可以得到我們的需要的位址。

在core location框架下,使用clgeocoderclplacemark就可以將cllocation中的資訊解析成位址。

拿上面的**方法中得到的cllocation例項做解析物件。

clgeocoder* geocoder = [[clgeocoder alloc] init];

[geocoder reversegeocodelocation:location completionhandler:^(nsarray* _nullable placemarks, nserror * _nullable error) ];

先看一下輸出的結果。

2016-03-08 15:58:03.076 latitude +30.283291, longitude +120.115639

2016-03-08 15:58:09.841 西湖國際科技大廈d座

clplacemark還有許多與該位址相關的屬性,可以輸出看一下。

2016-03-08 16:04:09.645 name:西湖國際科技大廈d座

2016-03-08 16:04:09.646 isocountrycode:cn

2016-03-08 16:04:09.646 country:中國

2016-03-08 16:04:09.647 postalcode:(null)

2016-03-08 16:04:09.647 administrativearea:浙江省

2016-03-08 16:04:09.647 subadministrativearea:(null)

2016-03-08 16:04:09.647 locality:杭州市

2016-03-08 16:04:09.647 sublocality:西湖區

2016-03-08 16:04:09.647 thoroughfare:文二路391西湖國際科技大廈

2016-03-08 16:04:09.648 subthoroughfare:(null)

2016-03-08 16:04:09.648 region:clcircularregion (identifier:' radius 197.16', center:, radius:197.16m)

2016-03-08 16:04:09.649 timezone:asia/shanghai (gmt+8) offset 28800

在結果中我們可以看出:

CoreLocation基本使用

匯入框架,匯入標頭檔案,引入全域性cllocationmanager物件 因為整個工程都要用到它,不能讓他死 設定 self.manager.delegate self 設定多久獲取一次 self.manager.distancefilter 500 設定獲取位置的精確度 self.manager....

ios 使用Core Location定位

core location可以利用三種技術來實現 gps 蜂窩基站三角網定位 cell tower triangulation 和wi fi定位服務 wps 其中gps是最精確的。我們只用告訴core location我們想要的精度級別,它將從它可用的技術中決定哪種可以更好地滿足你的需求。為了與co...

CoreLocation框架的使用

1.應用場景 2.ios中加入定位和地圖功能所依賴的框架 mapkit 3.兩個熱門專業術語 4.使用corelocation框架進行定位1.為什麼了解 2.ios8.0之前的前台定位 建立cllocationmanager物件並設定 呼叫方法,開始更新使用者位置資訊 在對應的 方法中獲取位置資訊 ...