相關知識點 CLLocationManager
CLAuthorizationStatus
Accuracy Constants
distanceFilter
CLActivityType
開發(fā)環(huán)境 iOS 8.4
Xcode 6.4
API地址
簡述
The Core Location framework lets you determine the current location or heading associated with a device. The framework uses the available hardware to determine the user’s position and heading. You use the classes and protocols in this framework to configure and schedule the delivery of location and heading events. You can also use it to define geographic regions and monitor when the user crosses the boundaries of those regions. In iOS, you can also define a region around a Bluetooth beacon.
根據(jù)蘋果官方文檔的描述虎囚,Core Location Framework的用途是使用戶通過移動設備來獲取定位信息和方向信息谦秧,還有你的范圍,當用戶走過某些范圍邊界就能馬上監(jiān)控到阵面,在iOS上绣否,甚至可以連同beacon聯(lián)動來確定周邊信息誊涯。
現(xiàn)在我主要是通過一些案例來學習這個Framework積累經(jīng)驗,寫得不好的話蒜撮,麻煩吐槽
開始——
首先新建一個項目,這篇文件是講Core Location Framework暴构,那當然要引入這個Framework,引入后我們在新建項目的ViewController.h引入這個框架ViewController.m
引入#import <CoreLocation/CoreLocation.h>
并且添加CLLocationManagerDelegate
然后按著Command點擊<CoreLocation/CoreLocation.h>,進入看看框架里面的內(nèi)容,里面就包含了以下這些Class
在viewDidLoad
函數(shù)里面取逾,我們添加以下這段代碼
/************ 初始化地理位置~begin **************/
if (locationManager == nil){
locationManager = [[CLLocationManager alloc] init];
//要求的精準度
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
//設置最小距離范圍數(shù)據(jù)更新耗绿,單位為米,例如:10.0f為如果水平移動超過10米范圍則會更新地理位置信息
locationManager.distanceFilter = 10.0f;
/**
* @author 老區(qū)
*
* 這個值是和更新位置信息有關砾隅,當一定時間范圍內(nèi)沒有檢測用戶的位置變化的話缭乘,則自動暫停位置服務,等到位置發(fā)生變化后才喚醒琉用,這個目的是為了節(jié)省系統(tǒng)電量
* 默認值為CLActivityTypeOther
* CLActivityTypeAutomotiveNavigation, // 汽車使用
* CLActivityTypeFitness, // 徒步使用
* CLActivityTypeOtherNavigation // 船,火車策幼,飛機使用
*/
locationManager.activityType = CLActivityTypeAutomotiveNavigation;
locationManager.delegate = self;
}
//檢查授權邑时,如果沒有這句判斷的話在iOS執(zhí)行會直接出現(xiàn)"unknown selector"
if ([locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
[locationManager requestAlwaysAuthorization];//for iOS 8
}
//獲取地址位置
[locationManager startUpdatingLocation];
/************ 初始化地理位置~end **************/
CLLocationManager
The CLLocationManager
class is the central point for configuring the delivery of location- and heading-related events to your app. You use an instance of this class to establish the parameters that determine when location and heading events should be delivered and to start and stop the actual delivery of those events. You can also use a location manager object to retrieve the most recent location and heading data.
這里意思是傳遞location和heading相關事件到APP上顯示。還有就是控制開啟和關閉獲取地址位置信息和數(shù)據(jù)特姐,取回之前的位置和方向數(shù)據(jù)晶丘。
這里有幾點要注意的地方,就是使用CLLocationManager會詢問獲取定位功能的授權Requesting Permission to Use Location Services
,我們可以根據(jù)[Constants]CLAuthorizationStatus來查看其授權狀態(tài)唐含,
CLAuthorizationStatus(授權狀態(tài) )
這里分別有以下幾種授權狀態(tài)——
kCLAuthorizationStatusNotDetermined
——第一次安裝應用的時候用戶還沒選擇是否使用定位服務功能kCLAuthorizationStatusRestricted
——這個App的定位服務功能受限浅浮,用戶無法改變這個App的狀態(tài),有可能是由于開啟某些限制例如家長控制之類的操作kCLAuthorizationStatusDenied
——用戶明細地拒絕此App使用定位服務功能或者在用戶設置那里禁用定位服務kCLAuthorizationStatusAuthorizedAlways
——該APP授權在App任何時候包括前端運行和后端運行使用定位服務kCLAuthorizationStatusAuthorizedWhenInUse
——該APP授權在App前端運行的時候使用定位服務
其中這里的兩種授權狀態(tài)WhenInUse
和Always
都能獲取定位服務信息捷枯,但WhenInUse
只允許用戶在App在foreground(前臺運行)狀態(tài)下才能獲取位置數(shù)據(jù)滚秩,而Always
是任何時候,包括在foreground和background狀態(tài)下都能獲取到數(shù)據(jù)
重要:如果是iOS 8或之后淮捆,你還需要添加 NSLocationWhenInUseUsageDescription和NSLocationAlwaysUsageDescription到Info.plist上才會彈出授權提示
//檢查授權郁油,如果沒有這句判斷的話在iOS執(zhí)行會直接出現(xiàn)"unknown selector"
if ([locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
[locationManager requestAlwaysAuthorization];//for iOS 8
}
注意:如果您是先使用
requestWhenInUseAuthorization
來獲取授權的話,之后再使用requestAlwaysAuthorization
獲取授權的話攀痊,會彈出提示用戶變更授權狀態(tài)桐腌,但反過來則不行,我認為是Always
這個授權是高于WhenInUse
苟径,如果你真的要將Always
改為WhenInUse
的話案站,則可以通過Setting那里去設置
Accuracy Constants
//要求的精準度
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
這里我大概截了下圖片,知道有哪些常量就OK
CLActivityType
相關代碼說明:
/**
* @author 老區(qū)
*
* 這個值是和更新位置信息有關棘街,當一定時間范圍內(nèi)沒有檢測用戶的位置變化的話蟆盐,則自動暫停位置服務,等到位置發(fā)生變化后才喚醒蹬碧,這個目的是為了節(jié)省系統(tǒng)電量
* 默認值為CLActivityTypeOther
* CLActivityTypeAutomotiveNavigation, // 汽車使用
* CLActivityTypeFitness, // 徒步使用
* CLActivityTypeOtherNavigation // 船舱禽,火車,飛機使用
*/
locationManager.activityType = CLActivityTypeOther;
distanceFilter
//設置最小距離范圍數(shù)據(jù)更新恩沽,單位為米誊稚,例如:10.0f為如果水平移動超過10米范圍則會更新地理位置信息
locationManager.distanceFilter = 10.0f;
其中還有一個kCLDistanceFilterNone
,這個常量的意思是沒有任何范圍限制,只要位置發(fā)生任何變化都執(zhí)行更新,但這樣會很耗電
最后——
這些都準備好之后執(zhí)行startUpdatingLocation
來獲取定位信息
[locationManager startUpdatingLocation];
當成功授權后里伯,便可以通過- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
這個Delegate來獲取位置數(shù)據(jù)
#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation *newLocation = [locations lastObject];
NSLog(@"newLocation lat:%f", newLocation.coordinate.latitude);
NSLog(@"newLocation lng:%f", newLocation.coordinate.longitude);
}
相關參考文章:
http://nevan.net/2014/09/core-location-manager-changes-in-ios-8/
原創(chuàng)作者:老區(qū)
Email: leo.au@foxmail.com
QQ:81508056
微信ID:kingOU