NS_CLASS_AVAILABLE(10_6, 2_0) @interface CLLocationManager : NSObject
配置git
一、在 iOS7 及之前的版本,若是在應用程序中使用定位服務只要在程序中調用 startUpdatingLocation
方法應用就會詢問用戶是否容許此應用是否容許使用定位服務,同時在提示過程當中能夠經過在 info.plist 中配置經過配置 Privacy - Location Usage Description
告訴用戶使用的目的,同時這個配置是可選的。atom
可是在 iOS8 中配置項發生了變化,能夠經過配置 Privacy - Location Always Usage Description (NSLocationAlwaysUsageDescription)
或者 Privacy - Location When In Use Usage Description(NSLocationWhenInUseUsageDescription)
來告訴用戶使用定位服務的目的,而且注意這個配置是必須的,若是不進行配置則默認狀況下應用沒法使用定位服務,打開應用不會給出打開定位服務的提示,除非安裝後本身設置此應用的定位服務。同時,在應用程序中須要根據配置對 requestAlwaysAuthorization
或 requestWhenInUseAuthorization
方法進行請求。代理
二、在須要使用 CoreLocation 的文件中code
// 包含頭文件 #import <CoreLocation/CoreLocation.h> // 遵照協議 <CLLocationManagerDelegate>
建立開啓定位請求blog
// 聲明定位管理器 @property (nonatomic, strong) CLLocationManager *locationManager; // 實例化定位管理器 self.locationManager = [[CLLocationManager alloc] init]; // 設置代理 self.locationManager.delegate = self; // 判斷系統定位服務是否開啓 if (![CLLocationManager locationServicesEnabled]) { // 建立警告框(自定義方法) [self showAlertWithTitle:@"提示" message:@"系統定位服務不可用,請開啓 !"]; } else { // 判斷應用定位服務受權狀態 if([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined){ // 沒有受權 // 8.0 及以上系統需手動請求定位受權 if ([UIDevice currentDevice].systemVersion.doubleValue >= 8.0) { // 設置前臺定位,需在 info.plist 裏設置 Privacy - Location When In Use Usage Description 的值 [self.locationManager requestWhenInUseAuthorization]; // 設置先後臺同時定位,需在 info.plist 裏設置 Privacy - Location Always Usage Description 的值 // [self.locationManager requestAlwaysAuthorization]; } // 開始定位追蹤(第一次打開軟件時) [self.locationManager startUpdatingLocation]; } else if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorizedWhenInUse || [CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorizedAlways) { // 容許定位受權 // 開始定位追蹤 [self.locationManager startUpdatingLocation]; } else{ // 拒絕定位受權 // 建立警告框(自定義方法) [self showAlertWithTitle:@"提示" message:@"當前應用的定位服務不可用,請檢查定位服務受權狀態 !"]; } }
獲取定位結果ip
// 定位到位置 // CLLocationManagerDelegate 協議方法 - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{ CLLocation *location = [locations lastObject]; // 經緯度 CLLocationDegrees longitude = location.coordinate.longitude; // 經度 CLLocationDegrees latitude = location.coordinate.latitude; // 緯度 // 海拔 CLLocationDistance altitude = location.altitude; // 路線,航向(0.0 度~359.9 度,0.0 度表明真北方向) CLLocationDirection course = location.course; // 速度(m/s) float speed = location.speed; // 中止定位(若是不關閉,會一直處在定位請求中) [manager stopUpdatingLocation]; }
// 設置代理 self.locationManager.delegate = self; // 獲取系統定位服務開啓狀態 BOOL isLocationServicesEnabled = [CLLocationManager locationServicesEnabled]; // 獲取應用定位服務受權狀態 CLAuthorizationStatus authorizationStatus = [CLLocationManager authorizationStatus]; // 設置每隔多少米更新位置信息 self.locationManager.distanceFilter = kCLDistanceFilterNone; // 設置定位精確度 self.locationManager.desiredAccuracy = kCLLocationAccuracyBest; // 設置前臺定位,需在 info.plist 裏設置 Privacy - Location When In Use Usage Description 的值 [self.locationManager requestWhenInUseAuthorization]; // 設置先後臺同時定位,需在 info.plist 裏設置 Privacy - Location Always Usage Description 的值 [self.locationManager requestAlwaysAuthorization]; // 開始定位 [self.locationManager startUpdatingLocation]; // 中止定位(若是不關閉,會一直處在定位請求中) [self.locationManager stopUpdatingLocation]; // 獲取定位到的 經緯度 CLLocationDegrees longitude = location.coordinate.longitude; // 經度 CLLocationDegrees latitude = location.coordinate.latitude; // 緯度 // 獲取定位到的 海拔 CLLocationDistance altitude = location.altitude; // 獲取定位到的 路線,航向(0.0 度~359.9 度,0.0 度表明真北方向) CLLocationDirection course = location.course; // 獲取定位到的 速度(m/s) float speed = location.speed; // 計算兩個位置之間的距離 CLLocation *location1 = [[CLLocation alloc] initWithLatitude:40 longitude:116]; CLLocation *location2 = [[CLLocation alloc] initWithLatitude:41 longitude:116]; CLLocationDistance distance = [location1 distanceFromLocation:location2];