iOS-定位

使用地圖的CLLocationManager完成定位效果.
例子:定位當(dāng)前所在位置,把所在城市的城市名顯示在label上

IMG_0111.PNG
Simulator Screen Shot 2016年5月25日 下午11.18.49.png

實現(xiàn)定位效果步驟如下:
1.導(dǎo)入頭文件 <CoreLocation/CoreLocation.h>
2.創(chuàng)建位置管理者 CLLocationManager 的對象,并設(shè)置代理
3.實現(xiàn)代理對應(yīng)協(xié)議的方法
4.使用系統(tǒng)自帶的 CLGeocoder 類,將經(jīng)度和緯度解析成地名

代碼實現(xiàn)如下:

1.導(dǎo)入頭文件

#import <CoreLocation/CoreLocation.h>

2.創(chuàng)建位置管理者,和 CLGeocoder 類的對象.這里把他設(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文件,添加兩個字段,分別是 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以后新的請求定位方法,不能與startUpdatingLocation同時使用
   // [self.LManager requestLocation];

讓當(dāng)前類遵從CLLocationManagerDelegate協(xié)議,并實現(xiàn)協(xié)議里面的一些方法

CLLocationManagerDelegate
定位失敗時調(diào)用

/**  定位失敗時調(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)改變時調(diào)用

/** 定位服務(wù)狀態(tài)改變時調(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(@"獲得前后臺授權(quán)");
            break;
        }
        case kCLAuthorizationStatusAuthorizedWhenInUse: {
            NSLog(@"獲得前后臺授權(quán)");
            break;
        }
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容