//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学习的第二十三天

    1.今天学习了用log4j记录日志 综合实例 2.不知道日志什么时候用到 3.明天学习12章

  2. PHP实现页面静态化的简单方法分享

    为什么要页面静态化? 1.动态文件执行过程:语法分析-编译-运行 2.静态文件,不需要编译,减少了服务器脚本运行的时间,降低了服务器的响应时间,直接运行,响应速度快:如果页面中一些内容不经常改动,动态 ...

  3. python给图片打马赛克

    图片马赛克 关注公众号"轻松学编程"了解更多. 导包 import numpy as np import matplotlib.pyplot as plt %matplotlib ...

  4. CF716D Complete The Graph

    图论+构造 首先可以发现如果去除了可以改变权值的边,$s$到$t$的最短路若小于$l$,那么一定不行 若等于则直接将可改边权的边改为inf,输出即可 那么现在原图中的最短路是大于$l$的 因为每一条边 ...

  5. 推荐系统,深度论文剖析GBDT+LR

    今天我们来剖析一篇经典的论文:Practial Lessons from Predicting Clicks on Ads at Facebook.从这篇paper的名称当中我们可以看得出来,这篇pa ...

  6. maven 搭建私服

    博客参考 https://www.cnblogs.com/luotaoyeah/p/3791966.html 整理纯为技术学习 1 . 私服简介 私服是架设在局域网的一种特殊的远程仓库,目的是代理远程 ...

  7. Spark Standalone模式 高可用部署

      本文使用Spark的版本为:spark-2.4.0-bin-hadoop2.7.tgz. spark的集群采用3台机器进行搭建,机器分别是server01,server02,server03. 其 ...

  8. JS多物体运动案例:变宽、变高

    任务描述: 当鼠标移入"变宽"矩形时,该矩形宽度逐渐增加至400px,移出该矩形,宽度逐渐恢复至初始值;当鼠标移入"变高"矩形时,该矩形高度逐渐增加至400px ...

  9. 客户的一个紧急bug,我用了两种方式进行 C# 反编译修改源码

    一:背景 1. 讲故事 周五下午运营反馈了一个紧急bug,说客户那边一个信息列表打不开,急需解决,附带的日志文件也发过来了,看了下日志大概是这样的: 日期:2020-11-13 12:25:45,92 ...

  10. http 结构初始化

    简要而说:accept 到连接后 根据fd 构建一个connection  由于是 http : 重新封装为http-connection:同时设置fd的读回调: 回调函数根据是否是https/htt ...