使用地圖的CLLocationManager完成定位效果.
例子:定位當(dāng)前所在位置,把所在城市的城市名顯示在label上
IMG_0111.PNG
Simulator Screen Shot 2016年5月25日 下午11.18.49.png
實(shí)現(xiàn)定位效果步驟如下:
1.導(dǎo)入頭文件 <CoreLocation/CoreLocation.h>
2.創(chuàng)建位置管理者 CLLocationManager 的對(duì)象,并設(shè)置代理
3.實(shí)現(xiàn)代理對(duì)應(yīng)協(xié)議的方法
4.使用系統(tǒng)自帶的 CLGeocoder 類,將經(jīng)度和緯度解析成地名
代碼實(shí)現(xiàn)如下:
1.導(dǎo)入頭文件
#import <CoreLocation/CoreLocation.h>
2.創(chuàng)建位置管理者,和 CLGeocoder 類的對(duì)象.這里把他設(shè)置成屬性
/** 位置管理者*/
@property (nonatomic,strong) CLLocationManager *LManager ;
/** 將經(jīng)度和緯度解析成地名*/
@property (nonatomic,strong) CLGeocoder *geocoder;
使用懶加載初始化
//懶加載
- (CLLocationManager *)LManager{
if (!_LManager) {
//創(chuàng)建位置管理者
_LManager = [[CLLocationManager alloc] init];
_LManager.delegate = self;
}
return _LManager;
}
- (CLGeocoder *)geocoder{
if (!_geocoder) {
_geocoder = [[CLGeocoder alloc] init];
}
return _geocoder;
}
在這里需要知道,定位在進(jìn)入iOS8之后,需要修改plist文件,添加兩個(gè)字段,分別是 NSLocationAlwaysUsageDescription 以及 NSLocationWhenInUseDescription 如下圖所示
11.png
由于只有定位需求,所以在viewDidLoad方法里面直接寫定位方法.
首先判斷用戶是否關(guān)閉了定位
//判斷用戶是否關(guān)閉了定位
if (![CLLocationManager locationServicesEnabled]) {
NSLog(@"定位失敗");
return;
}
版本適配
//做版本適配(ios8以后才有的方法,所以要做適配)
if ([self.LManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
[self.LManager requestWhenInUseAuthorization];
}
使用位置管理者,開始更新用戶位置
//使用位置管理者,開始更新用戶位置
[self.LManager startUpdatingLocation];
//iOS9.0以后新的請(qǐng)求定位方法,不能與startUpdatingLocation同時(shí)使用
// [self.LManager requestLocation];
讓當(dāng)前類遵從CLLocationManagerDelegate協(xié)議,并實(shí)現(xiàn)協(xié)議里面的一些方法
CLLocationManagerDelegate
定位失敗時(shí)調(diào)用
/** 定位失敗時(shí)調(diào)用*/
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
NSLog(@"定位失敗");
}
位置更新后調(diào)用
/** 位置更新后調(diào)用*/
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
CLLocation *location = [locations lastObject];
[self.geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
for (CLPlacemark *placemark in placemarks) {
for (NSString *key in placemark.addressDictionary) {
NSLog(@"%@ = %@",key,placemark.addressDictionary[key]);
[self.label performSelectorOnMainThread:@selector(setText:) withObject:placemark.addressDictionary[@"City"] waitUntilDone:YES];
}
}
}];
}
定位服務(wù)狀態(tài)改變時(shí)調(diào)用
/** 定位服務(wù)狀態(tài)改變時(shí)調(diào)用*/
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{
switch (status) {
case kCLAuthorizationStatusNotDetermined: {
NSLog(@"用戶還未決定授權(quán)");
if ([manager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
[manager requestAlwaysAuthorization];
}
break;
}
case kCLAuthorizationStatusRestricted: {
NSLog(@"訪問受限");
break;
}
case kCLAuthorizationStatusDenied: {
if ([CLLocationManager locationServicesEnabled]) {
NSLog(@"定位服務(wù)開啟,被拒絕");
}
else{
NSLog(@"定位服務(wù)關(guān)閉,不可用");
}
break;
}
case kCLAuthorizationStatusAuthorizedAlways: {
NSLog(@"獲得前后臺(tái)授權(quán)");
break;
}
case kCLAuthorizationStatusAuthorizedWhenInUse: {
NSLog(@"獲得前后臺(tái)授權(quán)");
break;
}
}
}