ios---CoreLocation框架实现定位功能
CoreLocation框架实现定位功能(iOS8.0之后)
//
// ViewController.m
// 定位
//
// Created by admin on 2017/9/20.
// Copyright © 2017年 admin. All rights reserved.
//
#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>
@interface ViewController ()<CLLocationManagerDelegate>
@property(nonatomic,strong)CLLocationManager *locationManager;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self startLocation];
}
-(void)startLocation
{
//判断用户是否打开了定位功能
if([CLLocationManager locationServicesEnabled]){
if(!_locationManager){
_locationManager=[[CLLocationManager alloc]init];
//设置代理
[self.locationManager setDelegate:self];
//设置定位精确度,精确度越高,越耗电
[self.locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
//设置多远距离定位一次
[self.locationManager setDistanceFilter:100];
//开始获取授权,打开定位
[self.locationManager requestWhenInUseAuthorization];
//开始定位
[self.locationManager startUpdatingLocation];
}else{
[self.locationManager startUpdatingLocation];
}
}else{
NSLog(@"%d",666);
}
}
#pragma mark -CLLocationManagerDelegate
//代理方法监听定位服务状态的变化
-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{
switch (status) {
case kCLAuthorizationStatusNotDetermined:
if([_locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]){
[_locationManager requestAlwaysAuthorization];
NSLog(@"用户还未决定授权");
}
break;
case kCLAuthorizationStatusAuthorizedWhenInUse:
if([_locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]){
[_locationManager requestAlwaysAuthorization];
NSLog(@"定位服务授权状态被允许在使用应用程序的时候");
}
break;
case kCLAuthorizationStatusRestricted:
{
NSLog(@"访问受限");
break;
}
case kCLAuthorizationStatusAuthorizedAlways:
if([_locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]){
[_locationManager requestAlwaysAuthorization];
NSLog(@"定位服务授权状态已经被用户允许在任何状态下获取位置信息。包括监测区域、访问区域、或者在有显著的位置变化的时候");
}
break;
case kCLAuthorizationStatusDenied:
NSLog(@"被拒绝了");
break;
default:
break;
}
}
//代理方法返回locationd 信息
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
NSLog(@"%@",locations);
CLLocation *currLocation=locations.lastObject;
NSTimeInterval locatinAge=-[currLocation.timestamp timeIntervalSinceNow];
NSLog(@"%f----%f",[currLocation.timestamp timeIntervalSince1970],locatinAge);
//关闭定位
[self.locationManager stopUpdatingLocation];
CLLocation *location=locations.lastObject;
[self reverseGeocoder:location];
}
//地理反编码
-(void)reverseGeocoder:(CLLocation *)currentLocation{
CLGeocoder *geocoder=[[CLGeocoder alloc]init];
[geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
if(error || placemarks.count==0){
NSLog(@"反编码失败");
}else{
CLPlacemark *placemark=placemarks.firstObject;
NSLog(@"placemark:%@",[placemark addressDictionary]);
NSString *city=[[placemark addressDictionary]objectForKey:@"City"];
NSLog(@"%@",city);
}
}];
}
@end
github:https://github.com/Frankltf/ios-CoreLocation/tree/features-one
ios---CoreLocation框架实现定位功能的更多相关文章
- IOS CoreLocation框架的使用(用于地理定位)
● 在移动互联网时代,移动app能解决用户的很多生活琐事,比如 ● 导航:去任意陌生的地方 ● 周边:找餐馆.找酒店.找银行.找电影院 ● 在上述应用中,都用到了地图和定位功能,在iOS开发中 ...
- ios - 自动布局框架编写(更多功能完善中)
之前用的storyboard以及xib挺多的,最近看到朋友用第三方框架---自动布局约束框架在添加控件约束的时候老实报错.后来自己就试了试纯代码创建以及约束控件.但是纯代码约束一个控件还可以,如果约束 ...
- Flex AIR应用GPS定位功能(Android和IOS)
说明: 使用AIR进行GPS定位功能实现时,会经常判断GPS是否打开.一般的官方或者书上的介绍的方法,测试后,只能对Android系统进行判断,而对ios系统则无法进行判断. 经过研究测试,终于解决实 ...
- CoreLocation框架的使用
CoreLocation框架使用 一.地图和定位的简介 1.应用场景 周边:找餐馆/找KTV/找电影院(团购APP) 导航:根据用户设定的起点和终点,进行路线规划,并指引用户如何到达(地图APP) 2 ...
- iOS常用框架源码分析
SDWebImage NSCache 类似可变字典,线程安全,使用可变字典自定义实现缓存时需要考虑加锁和释放锁 在内存不足时NSCache会自动释放存储的对象,不需要手动干预 NSCache的key不 ...
- 【iOS】7.4 定位服务->2.1.3.1 定位 - 官方框架CoreLocation 功能1:地理定位
本文并非最终版本,如果想要关注更新或更正的内容请关注文集,联系方式详见文末,如有疏忽和遗漏,欢迎指正. 本文相关目录: ================== 所属文集:[iOS]07 设备工具 === ...
- 【iOS】7.4 定位服务->2.1.3.2 定位 - 官方框架CoreLocation 功能2:地理编码和反地理编码
本文并非最终版本,如果想要关注更新或更正的内容请关注文集,联系方式详见文末,如有疏忽和遗漏,欢迎指正. 本文相关目录: ================== 所属文集:[iOS]07 设备工具 === ...
- 【iOS】7.4 定位服务->2.1.3.3 定位 - 官方框架CoreLocation 功能3:区域监听
本文并非最终版本,如果想要关注更新或更正的内容请关注文集,联系方式详见文末,如有疏忽和遗漏,欢迎指正. 本文相关目录: ================== 所属文集:[iOS]07 设备工具 === ...
- iOS定位--CoreLocation框架
CoreLocation框架的使用 // 首先导入头文件 #import <CoreLocation/CoreLocation.h> CoreLocation框架中所有数据类型的前缀都是C ...
随机推荐
- SpringJDBC的使用(转载)
转载自 https://www.yiibai.com/spring/maven-spring-jdbc-example.html 工具: eclipse4.7.2及mysql-8.0.13 项目最 ...
- 【题解】284E. Coin Troubles(dp+图论建模)
[题解]284E. Coin Troubles(dp+图论建模) 题意就是要你跑一个完全背包,但是要求背包的方案中有个数相对大小的限制 考虑一个\(c_i<c_j\)的限制,就是一个\(c_i\ ...
- 大数据学习之路-hdfs
1.什么是hadoop hadoop中有3个核心组件: 分布式文件系统:HDFS —— 实现将文件分布式存储在很多的服务器上 分布式运算编程框架:MAPREDUCE —— 实现在很多机器上分布式并行运 ...
- 你确定你了解什么是linux系统?
1.什么是linux发行版 就Linux的本质来说,它只是操作系统的核心,负责控制硬件.管理文件系统.程序进程等,并不给用户提供各种工具和应用软件.所谓工欲善其事,被必先利其器,一套在优秀的操作系统核 ...
- Exceptionless运用结果
一.后台页面功能 列表菜单 SubmitLog - 记录一般日志 log Messages SubmitException - 记录一次日志 Exceptions SubmitNotFound - 4 ...
- 输入URI,按下回车发生了什么?
当我们输入URL,按下回车发生了什么? 这个题目很俗套- -但是是面试经常出现的题目了.今天听尼古拉斯•屌•大斌哥介绍关于从URI到浏览器呈现给我们页面发生了什么.感觉收获颇多.索性就翻阅了一些其他资 ...
- iOS从gif获取图片数组
iOS中,当我们UIImageView实现动画时,如果图片是gif则不会自动播放gif图片,我们可以从gif图片中读取出每一帧的图片,然后组成图片数组,之后再实现使用UIImageView实现动画效果 ...
- 【GeneXus】在WorkWithPlus中如何定义未被包含的页面属性?
在使用GeneXus开发项目的过程中,有很多用户会使用到WorkWithPlus这个模板.通过WorkWithPlus的编辑器,让页面的调整变得极为简单,尤其是响应式页面.在WorkWithPlus的 ...
- Yolo V3整体思路流程详解!
结合开源项目tensorflow-yolov3(https://link.zhihu.com/?target=https%3A//github.com/YunYang1994/tensorflow-y ...
- Tasker如何使用Tasker插件以及Tasker第三方应用
很多人不清楚Tasker插件和Tasker第三方应用之间的区别,以及与Tasker的关系有何不同,其实对于使用者而言并不需要理解他们之间的区别,因为这两者在使用上的区别逐渐模糊而变得没有区别,不过本人 ...