获取地理位置信息(城市或者是省份)

最近看别人都写简书,觉得好高大上的样子,觉得像我这样作文水平告别及格的人,基本上就告别写文章了。问了下我的基友们(他们都是程序员,写的是技术文章),都说主要是给自己看,怕忘记。好吧,我也怕忘记,写一篇试试吧,反正是自己看,也顺便改善自己写代码不求甚解的不良习惯。

ps:这篇文主要是用系统自带的地图,获取,地理位置编码,然后反编码获取地理位置信息(这里是城市信息)

 一、准备工作

1、在info.plist里面添加权限(首先要➕App Transport Security Settings (字典类型),然后在该项下,再添加我下面说的类型)

a、Privacy - Location When In Use Usage Description(在使用从应用程序期间获取位置信息,我用的是这个)

b、Privacy - Location Always Usage Description(每次启用的时候,都要问问用户)

c、Privacy - Location Usage Description(这个据说是用户一旦选择拒绝,以后应用程序都不会使用用户的位置,也不会提示)

2、在Build Phases添加CoreFoundation的基础库

 二、代码

1、引入头文件

#import<CoreLocation/CoreLocation.h> //地理定位相关

2、属性


@property(nonatomic,strong)CLLocationManager *manager;

@property(nonatomic,strong)CLLocation *location;


3、代码

#pragma mark- Location- 定位服务

- (void)getLocation{

_manager = [[CLLocationManager alloc]init];

//设置代理

_manager.delegate=(id)self;

//设置定位精度

_manager.desiredAccuracy = kCLLocationAccuracyKilometer;

//定位频率,每隔多少米定位一次

CLLocationDistance distance=10.0f;//十米定位一次

_manager.distanceFilter=distance;

if (![CLLocationManager locationServicesEnabled]) {

//        [self showError:@"定位服务当前可能尚未打开"];

return;

}

if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined){

//ios8

if ([[UIDevice currentDevice].systemVersion floatValue]>=8.0) {

[_manager requestWhenInUseAuthorization];

//            [_manager requestAlwaysAuthorization];

}

//ios9

//                if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 9.0) {

//                    _manager.allowsBackgroundLocationUpdates = YES;

//                }

}else if([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorizedWhenInUse){

}

//启动跟踪定位

[_manager startUpdatingLocation];

}

4、获取位置之后的回调


#pragma mark-CLLocationManagerDelegate  位置更新后的回调

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations

{

//停止位置更新

[_manager stopUpdatingLocation];

//此处locations存储了持续更新的位置坐标值,取最后一个值为最新位置,如果不想让其持续更新位置,则在此方法中获取到一个值之后让locationManager stopUpdatingLocation

_location = [locations lastObject];

// 获取当前所在的城市名

CLGeocoder *geocoder = [[CLGeocoder alloc] init];

//根据经纬度反向地理编译出地址信息

[geocoder reverseGeocodeLocation:_location completionHandler:^(NSArray *array, NSError *error)

{

if (array.count > 0)

{

CLPlacemark *placemark = [array objectAtIndex:0];

//将获得的所有信息显示到label上

//            NSLog(@"placemark.name=%@,placemark.thoroughfare=%@,locality= %@,subLocality= %@,administrativeArea=%@,subAdministrativeArea= %@,postalCode = %@,ISOcountryCode= %@,country= %@,inlandWater=%@,ocean=%@",placemark.name,placemark.thoroughfare,placemark.locality,placemark.subLocality,placemark.administrativeArea,placemark.subAdministrativeArea,placemark.postalCode,placemark.ISOcountryCode,placemark.country,placemark.inlandWater,placemark.ocean);

//这是打印出来的信息2016-10-17 14:48:54.453 NewLive[7475:2760978] placemark.name=中国上海市长宁区虹桥街道中山西路1011号,placemark.thoroughfare=中山西路,locality= 上海市,subLocality= 长宁区,administrativeArea=上海市,subAdministrativeArea= (null),postalCode = (null),ISOcountryCode= CN,country= 中国,inlandWater=(null),ocean=(null)

//获取城市

NSString *city = placemark.locality;

if (!city) {

//四大直辖市的城市信息无法通过locality获得,只能通过获取省份的方法来获得(如果city为空,则可知为直辖市)

city = placemark.administrativeArea;

}

//            if (![NSString isEmpty:city]) {

//                NSString *searchString = city;

//                if (city.length>1) {

//                    NSString *temp = [city substringFromIndex:(city.length-1)];

//                    if ([@"市" isEqualToString:temp]) {

//                        searchString = [city substringToIndex:(city.length-1)];

//                    }

//                }

//                [MXEncryptionKeyStore storeKeyForeEncryption:searchString andWitchKey:LocationCity];

//            }

//            self.cityName = city;

}

else if (error == nil && [array count] == 0)

{

NSLog(@"No results were returned.");

}

else if (error != nil)

{

NSLog(@"An error occurred = %@", error);

}

}];

//系统会一直更新数据,直到选择停止更新,因为我们只需要获得一次经纬度即可,所以获取之后就停止更新

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • http://www.cnblogs.com/kenshincui/p/4125570.html 摘要: 现在很多...
    大崔老师阅读 3,314评论 1 2
  • CoreLocation框架 一. iOS8.0之前的定位(✨✨✨✨✨) 1. 前台定位 导入CoreLocati...
    0271fb6f797c阅读 546评论 0 1
  • 简介 在移动互联网时代,移动app能解决用户的很多生活琐事,比如 周边:找餐馆、找KTV、找电影院等等 导航:根据...
    JonesCxy阅读 1,251评论 1 1
  • 一. iOS8.0之前的定位(✨✨✨✨✨) 1. 前台定位 导入CoreLocation框架以及对应的主头文件 #...
    走道牙的人阅读 247评论 0 0
  • 又见你 漫天的星子 风也柔和 同你走过的小径 闻过的花香 还有露水打湿的衣裳 笑这痴傻的人儿 竟挽住岁月流转? 回...
    细雨天阅读 212评论 2 2