當App需要用戶的位置信息時,我們可以體統自帶的CoreLocation
來獲取,接下來就是所有的流程。
1.準備工作
1.1. 在info.plist 添加
Privacy - Location Always Usage Description
或者Privacy - Location When In Use Usage Description
權限,類型為String,value中一定要有值, 來告訴用戶使用定位服務的目的(一直定位/當用戶使用時定位)
1.2.在程序的
Build Phases
的Link Binary With Libraries
導入CoreLocation.framework
2.導入.m文件中,申請權限
在系統剛啟動時申請獲取用戶信息的權限,在AppDelegate.m 中申請使用用戶的地理位置權限,具體在哪需要地理位置信息,那就在哪實現獲取地理位置的相關代理。
下面的??是在AppDelegate.m 中申請的地理位置權限 ,在BaseController里實現代理方法。
@interface AppDelegate () <DBSessionDelegate,CLLocationManagerDelegate> //添加代理
@property (nonatomic, strong) CLLocationManager *cllocationManager;
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = [UIViewController new];
[self.window makeKeyAndVisible];
[self getUserLocation];
return YES;
}
- (void) getUserLocation {
int status=[CLLocationManager authorizationStatus];
if (![CLLocationManager locationServicesEnabled] || status < 3) {
if ([[UIDevice currentDevice].systemVersion floatValue] >= 8) {
_cllocationManager = [[CLLocationManager alloc] init];
[_cllocationManager requestAlwaysAuthorization];
[_cllocationManager requestWhenInUseAuthorization];
}
}
}
// 如果在Appdelegate中需要地理位置,那么在這實現代理方法來獲取地理位置信息
@end
在BaseController.m使用時實現代理方法
#import "BaseController.h"
#import <CoreLocation/CoreLocation.h>
@interface BaseController ()<CLLocationManagerDelegate>
@property (nonatomic, strong) CLLocationManager *locMgr;
@end
@implementation BaseController
#pragma mark-懶加載
- (CLLocationManager *)locMgr {
if (_locMgr==nil) {
//1.創建位置管理器(定位用戶的位置)
_locMgr=[[CLLocationManager alloc]init];
//2.設置代理
_locMgr.delegate=self;
}
return _locMgr;
}
- (void)viewDidLoad {
[super viewDidLoad];
//判斷用戶定位服務是否開啟
if ([CLLocationManager locationServicesEnabled]) {
//開始定位用戶的位置
[self.locMgr startUpdatingLocation];
//每隔多少米定位一次(這里的設置為任何的移動)
self.locMgr.distanceFilter=kCLDistanceFilterNone;
//設置定位的精準度,一般精準度越高,越耗電(這里設置為精準度最高的,適用于導航應用)
self.locMgr.desiredAccuracy=kCLLocationAccuracyBestForNavigation;
} else {
//不能定位用戶的位置
//1.提醒用戶檢查當前的網絡狀況
//2.提醒用戶打開定位開關
}
//測試方法,計算兩個位置之間的距離
[self countDistance];
}
#pragma mark - CLLocationManagerDelegate
/**
* 當定位到用戶的位置時,就會調用(調用的頻率比較頻繁)
*/
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
//locations數組里邊存放的是CLLocation對象,一個CLLocation對象就代表著一個位置,獲取最后的一個位置
CLLocation *currentLocation = [locations lastObject];
//當定位成功后,如果horizontalAccuracy大于0,說明定位有效 horizontalAccuracy,該位置的緯度和經度確定的圓的中心,并且這個值表示圓的半徑。負值表示該位置的緯度和經度是無效的
//維度:currentLocation.coordinate.latitude
//經度:currentLocation.coordinate.longitude
CLLocationCoordinate2D coor = currentLocation.coordinate;
NSDictionary *location = @{@"latitude":@(coor.latitude),@"longitude":@(coor.longitude),@"horizontalAccuracy":@(currentLocation.horizontalAccuracy)};
[PBCacheUtil setUserLocation:location];
//停止更新位置(如果定位服務不需要實時更新的話,那么應該停止位置的更新)
[self.locMgr stopUpdatingLocation];
}
//獲取地理位置失敗,賦予默認值
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(nonnull NSError *)error {
NSDictionary *location = @{@"latitude":@(0.000000),@"longitude":@(0.000000),@"horizontalAccuracy":@(0.000000)};
[PBCacheUtil setUserLocation:location];
}
//計算兩個位置之間的距離
-(void)countDistance {
//根據經緯度創建兩個位置對象
CLLocation *loc1=[[CLLocation alloc]initWithLatitude:40 longitude:116];
CLLocation *loc2=[[CLLocation alloc]initWithLatitude:41 longitude:116];
//計算兩個位置之間的距離
CLLocationDistance distance=[loc1 distanceFromLocation:loc2];
NSLog(@"(%@)和(%@)的距離=%fM",loc1,loc2,distance);
}
@end