//1、添加定位管理委托协议 CLLocationManagerDelegate

 //2、初始化定位管理对象

self.locationManager=[[CLLocationManager alloc]init];

self.locationManager.delegate=self;

//定位精度

self.locationManager.desiredAccuracy=kCLLocationAccuracyBest;

//多长距离更新一次位置

self.locationManager.distanceFilter=50;

if (UIDevice.currentDevice.systemVersion.integerValue>8.0)

{

[self.locationManager requestAlwaysAuthorization];

[self.locationManager requestWhenInUseAuthorization];

}

 //开启定位服务

if (self.locationManager.locationServicesEnabled) {

[self.locationManager startUpdatingLocation];

}

 //3、调用系统定位方法

  

#pragma mark - CLLocation Delegate

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status

{

switch (status)

{

case kCLAuthorizationStatusNotDetermined:

if ([_locationManager respondsToSelector:@selector(requestAlwaysAuthorization)])

{

[_locationManager requestAlwaysAuthorization];

}

break;

default:

break;

}

}

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

{

//获取当前位置信息

CLLocation *location = [locations objectAtIndex:0];

//判断是否是在国内

if (![WGS84TOGCJ02 isLocationOutOfChina:[location coordinate]])

{

   //设置锁,防止并发写入

[[NSUserDefaults standardUserDefaults] synchronize];

//转换后的coord

CLLocationCoordinate2D coord = [WGS84TOGCJ02 transformFromWGSToGCJ:[location coordinate]];

_myCoordinate = coord;

[[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithFormat:@"%f",_myCoordinate.latitude] forKey:KCurrentLat];

[[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithFormat:@"%f",_myCoordinate.longitude] forKey:KCurrentLng];

}

//创建反地理编码对象

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

 //将经纬度信息转换成字符串位置信息

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

{

//         NSLog(@"placemark:%@",array);

if (array.count > 0)

{

CLPlacemark *placemark = [array objectAtIndex:0];

if (![DGFunction xfunc_check_strEmpty:placemark.locality])

{

[[ NSUserDefaults standardUserDefaults] setObject:placemark.locality forKey:@"localCity"];

[[NSUserDefaults standardUserDefaults] synchronize];

//                 NSDictionary *dic = @{@"city":placemark.locality};

if (![DGFunction xfunc_check_strEmpty:placemark.locality]) {

[[NSUserDefaults standardUserDefaults] setObject:placemark.administrativeArea forKey:k_Current_Province];

}

if (![DGFunction xfunc_check_strEmpty:placemark.locality]) {

[[NSUserDefaults standardUserDefaults] setObject:placemark.locality forKey:k_Current_City];

}

if (![DGFunction xfunc_check_strEmpty:placemark.subLocality]) {

[[NSUserDefaults standardUserDefaults] setObject:placemark.subLocality forKey:k_Current_Area];

}

[[NSUserDefaults standardUserDefaults] synchronize];

NSLog(@"locality:%@ subLocality:%@",placemark.locality,placemark.subLocality);

//关闭定位服务

      [_locationManager stopUpdatingLocation];

}

}

}];

}

ios中关于系统定位CLLocationManager的使用解析的更多相关文章

  1. ios 苹果原生系统定位 CLLocationManager

    首先要干这些事 下面的方法亲测可用 ------------------------------------------------------------ DNLogFUNC //初始化位置管理对象 ...

  2. IOS中调用系统的电话、短信、邮件、浏览功能

    iOS开发系列--通讯录.蓝牙.内购.GameCenter.iCloud.Passbook系统服务开发汇总 2015-01-13 09:16 by KenshinCui, 26990 阅读, 35 评 ...

  3. iOS - (利用/调用系统定位获取当前经纬度与地理信息)

    这些天做iOS项目的时候,需要通过定位来拿到当期城市的名称.百度地图SDK有这个功能,但为了不依赖第三方,这里使用iOS自带框架CoreLocation来实现这个需求.iOS8出来之后,针对定位需要多 ...

  4. iOS中获取系统相册中的图片

    一.获取单张图片 思路: 1.利用UIImagePickerController可以从系统自带的App(照片\相机)中获得图片 2.设置代理,遵守代理协议 注意这个UIImagePickerContr ...

  5. [BS-04] 在iOS中对系统定义的类的readonly属性可通过KVC进行赋值

    系统提供的类的readonly属性可通过KVC进行赋值 UITabBarController.h @interface UITabBarController : UIViewController &l ...

  6. iOS中调用系统录音功能及其播放

    最近做的项目中,用到了录音的功能,简单记录一下. 我的想法是:通过重写button的点击事件,来达到录音的目的. /*----------------------------------[录音]--- ...

  7. iOS中打印系统详细日志

    Q:如何打印当前的函数和行号? A:我们可以在打印时使用一些预编译宏作为打印参数,来打印当前的函数和行号.如: 1 NSLog(@"%s:%d obj=%@", __func__, ...

  8. iOS中 自定义系统相机 作者:韩俊强

    需要框架: #import <AVFoundation/AVFoundation.h> #import <AssetsLibrary/AssetsLibrary.h> 布局如下 ...

  9. IOS中调用系统拨打电话发送短信

    一.调用打电话界面 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat ...

随机推荐

  1. java安全编码指南之:序列化Serialization

    目录 简介 序列化简介 注意serialVersionUID writeObject和readObject readResolve和writeReplace 不要序列化内部类 如果类中有自定义变量,那 ...

  2. Jmeter——ForEach Controller&Loop Controller

    今天来分享下Jmeter中的2款循环控制器,ForEach Controller和Loop Controller,在使用上还是有所区别. ForEach Controller ForEach Cont ...

  3. pyspark计算最大值、最小值、平均值

    需求:使用pyspark计算相同key的最大值.最小值.平均值 说明: 最大值和最小值好计算,直接reduceByKey后使用python内置的max.min方法 平均值计算提供两种计算方法,直接先上 ...

  4. 一个名为不安全的类Unsafe

    最近为了更加深入了解NIO的实现原理,学习NIO的源码时,遇到了一个问题.即在WindowsSelectorImpl中的 pollWrapper属性,当我点进去查看它的PollArrayWrapper ...

  5. c#连接mysql答题步骤

    引用mysql数据库 using MySql.Data.MySqlClient; 有一个mysql.dll文件 下面是实例化连接数据库 MySqlConnection Conn = new MySql ...

  6. leetcode129valid-parentheses

    题目描述 给出一个仅包含字符'(',')','{','}','['和']',的字符串,判断给出的字符串是否是合法的括号序列 括号必须以正确的顺序关闭,"()"和"()[] ...

  7. 内网渗透 day9-免杀基础

    免杀1 目录 1. 会话提升的几种方式 1 2. armitage的熟悉 2 3. handler生成监听器的方法 6 4. 防止会话假死 6 5. 控制台设置编码 7 6. upx加壳 7 7. m ...

  8. 主题包含一张index.html

    有半年之久没有更新新作品了,但这个小小领地我并没有忘记,我会坚持下去,一直在这等你,等你的每次回眸,感恩你的每次驻足,这已经足够成为我坚守的动力和理由,尽管现在有很多不足和不尽人意,也没很多的时间管理 ...

  9. Spider--补充--Requests--session&cookie

    # session 与 cookie # 可能大家对session已经比较熟悉了,也大概了解了session的机制和原理,但是我们在做爬虫时如何会运用到session呢,就是接下来要讲到的会话保持. ...

  10. http 请求体数据处理2--ngx

    HTTP 处理数据包, 有的业务不需要,此时只需要将数据包文读取后丢弃, 但是ngx 为什么还要提供一个丢弃接口呢???解决了什么问题?? ------对于HTTP模块而言,放弃接收包体就是简单地不处 ...