ios中关于系统定位CLLocationManager的使用解析
//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的使用解析的更多相关文章
- ios 苹果原生系统定位 CLLocationManager
首先要干这些事 下面的方法亲测可用 ------------------------------------------------------------ DNLogFUNC //初始化位置管理对象 ...
- IOS中调用系统的电话、短信、邮件、浏览功能
iOS开发系列--通讯录.蓝牙.内购.GameCenter.iCloud.Passbook系统服务开发汇总 2015-01-13 09:16 by KenshinCui, 26990 阅读, 35 评 ...
- iOS - (利用/调用系统定位获取当前经纬度与地理信息)
这些天做iOS项目的时候,需要通过定位来拿到当期城市的名称.百度地图SDK有这个功能,但为了不依赖第三方,这里使用iOS自带框架CoreLocation来实现这个需求.iOS8出来之后,针对定位需要多 ...
- iOS中获取系统相册中的图片
一.获取单张图片 思路: 1.利用UIImagePickerController可以从系统自带的App(照片\相机)中获得图片 2.设置代理,遵守代理协议 注意这个UIImagePickerContr ...
- [BS-04] 在iOS中对系统定义的类的readonly属性可通过KVC进行赋值
系统提供的类的readonly属性可通过KVC进行赋值 UITabBarController.h @interface UITabBarController : UIViewController &l ...
- iOS中调用系统录音功能及其播放
最近做的项目中,用到了录音的功能,简单记录一下. 我的想法是:通过重写button的点击事件,来达到录音的目的. /*----------------------------------[录音]--- ...
- iOS中打印系统详细日志
Q:如何打印当前的函数和行号? A:我们可以在打印时使用一些预编译宏作为打印参数,来打印当前的函数和行号.如: 1 NSLog(@"%s:%d obj=%@", __func__, ...
- iOS中 自定义系统相机 作者:韩俊强
需要框架: #import <AVFoundation/AVFoundation.h> #import <AssetsLibrary/AssetsLibrary.h> 布局如下 ...
- IOS中调用系统拨打电话发送短信
一.调用打电话界面 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat ...
随机推荐
- Parameter 'name' implicitly has an 'any' type.
出现在vue3版本 找到tsconfig.json文件 增加"noImplicitAny":flase,或把"strict":true改成"stric ...
- Bootstrap 实现图片翻滚
今天给大家带来的是Bootstrap 实现的图片翻滚 效果图如下 点击左右箭头可以实现向左向右转动,这个功能在Bootstrap 官网和菜鸟教程上都有讲解,有点bootstrap基础的都能看明白 ,这 ...
- 测试php
/** * 测试guzzle * * @return void */ public function index() { $client = new GuzzleHttp\Client(); //12 ...
- Python如何快速复制序列?
1 基本用法 把序列乘以一个整数,就会产生一个新序列.这个新序列是原始序列复制了整数份,然后再拼接起来的结果. l=[1,2,3] l2=l * 3 logging.info('l2 -> %s ...
- martini-实例-脂质双分子层
Martini粗粒化模型一开始就是为脂质开发的.(http://jerkwin.github.io/2016/11/03/Martini%E5%AE%9E%E4%BE%8B%E6%95%99%E7%A ...
- linux服务器间配置ssh免密连接
先说一下,我用的centos7,root用户.ssh的原理就不说了,网上介绍的文章很多,直接开始说操作步骤吧: 1.首先确认有没有安装ssh,输入 rpm -qa |grep ssh查看 这样就表示安 ...
- Function(函数分享)第二节
一.类型注解 1.1 类型注解 函数的类型注解分为两个部分:参数类型注解和返回值类型注解.其中返回值类型注解有时候我们可以直接省略,因为Typescript可以根据返回的语句来自动判断出返回值的类型. ...
- 测试_QTP原理
QTP是基于GUI界面的自动化测试工具,用于系统的功能测试. QTP录制的是鼠标和键盘的消息.QTP录制回放时基于windows操作系统的消息机制.QTP在录制时监听应用程序的消息,监听到之后把消 ...
- 「NOIP2009」最优贸易 题解
「NOIP2009」最优贸易 题解 题目TP门 题目描述 \(C\)国有\(n\)个大城市和\(m\)条道路,每条道路连接这\(n\)个城市中的某两个城市.任意两个城市之间最多只有一条道路直接相连.这 ...
- SpringBoot 之 @ControllerAdvice 拦截异常并统一处理
在spring 3.2中,新增了@ControllerAdvice 注解,可以用于定义@ExceptionHandler.@InitBinder.@ModelAttribute,并应用到所有@Requ ...