iOS定位獲取國(guó)家和城市

1.在target->Build Phases->link Binary With Libraries中加入
CoreLocation.framework
2.在info.plist中加入
Privacy - Location Always Usage Description //總是開(kāi)啟 Privacy - Location When In Use Usage Description //在使用期間開(kāi)啟
3.在需要定位的頁(yè)面加入頭文件
#import <CoreLocation/CoreLocation.h>
#import <CoreLocation/CLLocationManagerDelegate.h>
4.聲明一個(gè)定位管理服務(wù)對(duì)象
@property(nonatomic ,strong)CLLocationManager *locationManager;
5.遵守定位管理服務(wù)協(xié)議
CLLocationManagerDelegate
6.在viewDidLoad中初始化定位服務(wù)對(duì)象- (void)viewDidLoad { [super viewDidLoad]; //定位服務(wù)初始化對(duì)象 _locationManager = [[CLLocationManager alloc]init ]; _locationManager.delegate = self; _locationManager.desiredAccuracy = kCLLocationAccuracyBest; _locationManager.distanceFilter = 1000.0f; if (![CLLocationManager locationServicesEnabled]) { NSLog(@"定位服務(wù)當(dāng)前可能尚未打開(kāi),請(qǐng)?jiān)O(shè)置打開(kāi)!"); return; }

  1. 實(shí)現(xiàn)CLLocationManagerDelegate 協(xié)議方法:
    - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations
    - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
    CLLocation *currentLocation = [locations lastObject]; CLGeocoder *geocoder = [[CLGeocoder alloc]init]; //反地理編碼 [geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
    MMLog(@"--array--%d---error--%@",(int)placemarks.count,error);
    if (placemarks.count > 0) {
    CLPlacemark *placemark = [placemarks objectAtIndex:0];
    NSString *city = placemark.administrativeArea;//獲取城市
    NSString *country = placemark.country;// 獲取國(guó)家
    NSLog(@"位于:%@",city);
    MMLog(@"%@",placemark.addressDictionary[@"Name"]);
    }
    }];
    }
    `
    這只是作為真機(jī)測(cè)試
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

  • 出自http://my.oschina.net/are1OfBlog/blog/420034 摘要 現(xiàn)在很多社交、...
    JJO閱讀 4,318評(píng)論 4 19
  • 一、定位介紹 現(xiàn)在很多社交、電商、團(tuán)購(gòu)應(yīng)用都引入了地圖和定位功能,似乎地圖功能不再是地圖應(yīng)用和導(dǎo)航應(yīng)用所特有的。的...
    LHsunshine閱讀 400評(píng)論 0 0
  • http://www.cnblogs.com/kenshincui/p/4125570.html 摘要: 現(xiàn)在很多...
    大崔老師閱讀 3,469評(píng)論 1 2
  • “艾米正能量”一直想通過(guò)我自己的努力、變化、成長(zhǎng)和思考給大家?guī)シe極正向的影響,只不過(guò)有時(shí),還是會(huì)產(chǎn)生些小情緒,希...
    艾米正能量閱讀 280評(píng)論 0 0
  • 昨天是6月7日,高考的日子,我在QQ群說(shuō)了一句話“離我兒子參加高考還有365天”,把大家都逗樂(lè)了。 今早看了一篇文...
    三倒拐閱讀 175評(píng)論 0 0

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