iOS定位获取国家和城市

1.在target->Build Phases->link Binary With Libraries中加入
CoreLocation.framework
2.在info.plist中加入
Privacy - Location Always Usage Description //总是开启 Privacy - Location When In Use Usage Description //在使用期间开启
3.在需要定位的页面加入头文件
#import <CoreLocation/CoreLocation.h>
#import <CoreLocation/CLLocationManagerDelegate.h>
4.声明一个定位管理服务对象
@property(nonatomic ,strong)CLLocationManager *locationManager;
5.遵守定位管理服务协议
CLLocationManagerDelegate
6.在viewDidLoad中初始化定位服务对象- (void)viewDidLoad { [super viewDidLoad]; //定位服务初始化对象 _locationManager = [[CLLocationManager alloc]init ]; _locationManager.delegate = self; _locationManager.desiredAccuracy = kCLLocationAccuracyBest; _locationManager.distanceFilter = 1000.0f; if (![CLLocationManager locationServicesEnabled]) { NSLog(@"定位服务当前可能尚未打开,请设置打开!"); return; }

  1. 实现CLLocationManagerDelegate 协议方法:
    - (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;// 获取国家
    NSLog(@"位于:%@",city);
    MMLog(@"%@",placemark.addressDictionary[@"Name"]);
    }
    }];
    }
    `
    这只是作为真机测试
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 出自http://my.oschina.net/are1OfBlog/blog/420034 摘要 现在很多社交、...
    JJO阅读 4,176评论 4 19
  • 一、定位介绍 现在很多社交、电商、团购应用都引入了地图和定位功能,似乎地图功能不再是地图应用和导航应用所特有的。的...
    LHsunshine阅读 347评论 0 0
  • http://www.cnblogs.com/kenshincui/p/4125570.html 摘要: 现在很多...
    大崔老师阅读 3,320评论 1 2
  • “艾米正能量”一直想通过我自己的努力、变化、成长和思考给大家带去积极正向的影响,只不过有时,还是会产生些小情绪,希...
    艾米正能量阅读 222评论 0 0
  • 昨天是6月7日,高考的日子,我在QQ群说了一句话“离我儿子参加高考还有365天”,把大家都逗乐了。 今早看了一篇文...
    三倒拐阅读 156评论 0 0