iOS開發(fā)隨筆——定位

前言

在iOS開發(fā)過程中經(jīng)常會使用到用戶位置信息,iOS系統(tǒng)封裝了定位所要的類庫

<CoreLocation/CoreLocation.h>

使用方法:

//首先需要設(shè)置代理
@interface ViewController ()<CLLocationManagerDelegate>

//其次需要聲明變量
@property (nonatomic, strong) CLLocationManager *manager;

//然后初始化變量manager
- (void)getLocation{
    _manager = [[CLLocationManager alloc]init];
    [_manager requestAlwaysAuthorization];
    _manager.delegate = self;
    [_manager startUpdatingLocation];
}

//最后重寫代理方法
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
    CLLocation *currLocation=[locations lastObject];
    NSLog(@"la---%f, lo---%f",currLocation.coordinate.latitude,currLocation.coordinate.longitude);
    // 使用CLGeocoder的做法,其實(shí)是因?yàn)閕os5開始,iphone推薦的做法。而MKReverseGeocoder在ios5之后,就不再推薦使用了,因?yàn)檫@個(gè)類需要實(shí)現(xiàn)兩個(gè)委托方法。而使用CLGeocodre,則可以使用直接的方法。
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    [geocoder reverseGeocodeLocation:currLocation completionHandler:^(NSArray* placemarks,NSError *error) {
        if (placemarks.count >0   ) {
            // 自動定位獲取城市等信息
            CLPlacemark * plmark = [placemarks objectAtIndex:0];
            NSLog(@"%@", plmark.name); //顯示所有地址
//            _label.text = plmark.name; //給label負(fù)值
        }
    }];
}

更加詳細(xì)的信息在這里,我也是從這里借鑒的,這里寫下是為了自己Mark

iOS 定位總結(jié)--roger_單

需要獲得地址詳細(xì)信息 在CLPlacemark *plmark中可以找到屬性plmark.addressDictionary

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

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

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