-
導入框架
Xcode中添加“CoreLocation.framework”
-
導入主頭文件
#import <CoreLocation/CoreLocation.h>
-
聲明管理器和代理
@interface ViewController ()<CLLocationManagerDelegate> @property (nonatomic, strong) CLLocationManager* locationManager; @end
-
初始化管理器
self.locationManager = [[CLLocationManager alloc] init]; self.locationManager.delegate = self; self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
-
開啟定位服務,需要定位時調用findMe方法:
- (void)findMe { /** 由于IOS8中定位的授權機制改變 需要進行手動授權 * 獲取授權認證,兩個方法: * [self.locationManager requestWhenInUseAuthorization]; * [self.locationManager requestAlwaysAuthorization]; */ if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) { NSLog(@"requestAlwaysAuthorization"); [self.locationManager requestAlwaysAuthorization]; } //開始定位,不斷調用其代理方法 [self.locationManager startUpdatingLocation]; NSLog(@"start gps"); }
-
代理設置
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations { // 1.獲取用戶位置的對象 CLLocation *location = [locations lastObject]; CLLocationCoordinate2D coordinate = location.coordinate; NSLog(@"緯度:%f 經度:%f", coordinate.latitude, coordinate.longitude); // 2.停止定位 [manager stopUpdatingLocation]; } - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error { if (error.code == kCLErrorDenied) { // 提示用戶出錯原因,可按住Option鍵點擊 KCLErrorDenied的查看更多出錯信息,可打印error.code值查找原因所在 } }
注意事項
-
iOS8中定位服務的變化:CLLocationManager協議方法不響應,無法回掉GPS方法,不出現獲取權限提示,解決方法如下,詳見1 詳見2
如果需要僅在前臺定位,你在調用startUpdatingLocation 前需要調用requestWhenInUseAuthorization(見設置步驟6) 如果需要在前后臺定位,你在調用startUpdatingLocation 前需要調用requestAlwaysAuthorization 在plist文件中添加NSLocationWhenInUseUsageDescription或(與)NSLocationAlwaysUsageDescription字段: 找到info.plist文件->右擊->Open As->Source Code->在尾部的</dict>標簽之前添加以下一個或兩個: <key>NSLocationWhenInUseUsageDescription</key><string>需要定位</string> <key>NSLocationAlwaysUsageDescription</key><string>需要定位</string>
-
用戶隱私的保護
從iOS 6開始,蘋果在保護用戶隱私方面做了很大的加強,以下操作都必須經過用戶批準授權:要想獲得用戶的位置,想訪問用戶的通訊錄、日歷、相機、相冊等;當想訪問用戶的隱私信息時,系統會自動彈出一個對話框讓用戶授權。 可以在Info.plist中設置NSLocationUsageDescription說明定位的目的(Privacy - Location Usage Description) 從iOS 8開始,用戶定位分兩種情況 總是使用用戶位置:NSLocationAlwaysUsageDescription 使用應用時定位:NSLocationWhenInUseDescription 當想訪問用戶的隱私信息時,系統會自動彈出一個對話框讓用戶授權 /** *打開定位服務 *需要在info.plist文件中添加(以下二選一,兩個都添加默認使用NSLocationWhenInUseUsageDescription): *NSLocationWhenInUseUsageDescription 允許在前臺使用時獲取GPS的描述 *NSLocationAlwaysUsageDescription 允許永遠可獲取GPS的描述
-
獲取當前軟件的定位服務狀態
if ([CLLocationManager locationServicesEnabled] //確定用戶的位置服務啟用 &&[CLLocationManager authorizationStatus]==kCLAuthorizationStatusDenied) //位置服務是在設置中禁用 { }
代碼:調用startLocation方法即可
#pragma mark Location and Delegate
- (void)startLocation
{
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
/** 由于IOS8中定位的授權機制改變 需要進行手動授權
* 獲取授權認證,兩個方法:
* [self.locationManager requestWhenInUseAuthorization];
* [self.locationManager requestAlwaysAuthorization];
*/
if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
NSLog(@"requestWhenInUseAuthorization");
// [self.locationManager requestWhenInUseAuthorization];
[self.locationManager requestAlwaysAuthorization];
}
//開始定位,不斷調用其代理方法
[self.locationManager startUpdatingLocation];
NSLog(@"start gps");
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations
{
// 1.獲取用戶位置的對象
CLLocation *location = [locations lastObject];
CLLocationCoordinate2D coordinate = location.coordinate;
NSLog(@"緯度:%f 經度:%f", coordinate.latitude, coordinate.longitude);
self.longitute = [NSNumber numberWithDouble:coordinate.longitude];
self.latitude = [NSNumber numberWithDouble:coordinate.latitude];
// 2.停止定位
[manager stopUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error
{
if (error.code == kCLErrorDenied) {
// 提示用戶出錯原因,可按住Option鍵點擊 KCLErrorDenied的查看更多出錯信息,可打印error.code值查找原因所在
}
}