iOS定位-核心定位框架CLLocation
2016-05-15 03:10
507 查看
原文链接:biggergao.github.io/CLLocation
我是前言
最近做了一下CLLocation相关的东西,较全面的写了点相关问题与解决办法,文章的demo可以在这里下载(有点RAC的知识)。如果是模拟器,运行时请选择Location GPX文件
祝学习愉快。
副本主要任务
定位设备经纬度与所在城市预备知识-CLLocation对象(可跳过)
CLLocation对象存储着CLLocationManager对象生成的位置数据,先介绍一下它的属性大概了解CLLocation是什么东西用于定位的属性 | 含义 |
---|---|
coordinate | 地理位置(经纬度) |
altitude | 海拔 |
floor | 建筑内逻辑楼层 |
timestamp | 定位时间戳 |
horizontalAccuracy | 水平技能范围,单位米(见注1) |
verticalAccuracy | 海拔误差,单位米 |
用于速度和方向的属性 | 含义 |
---|---|
speed | 瞬时速度 |
course | 设备移动方向 |
实战
1.模拟器参数设置(可跳过)
1.1添加GPX文件设置
修改latitude(经度)和longitude(纬度)的值,可以使用图上的值lat=”29.568863”和lon=”106.460922”,美丽山城重庆。
最后调试选择对应的GPX文件即可
1.2直接修改模拟器的值
修改参数即可
2.获取经纬度
2.1 iOS8前的BUG
我们需要在info.plist文件里添加两个字段给APP定位权限,不然在iOS8后是无法启动定位的。他们分别是属性名 | 含义 |
---|---|
NSLocationWhenInUseUsageDescription | 使用期间 |
NSLocationAlwaysUsageDescription | 始终开启 |
上个效果图好理解点:
2.2核心代码讲解
- (void)findCurrentLocation { self.isFirstUpdate = YES; // 1 if (! [CLLocationManager locationServicesEnabled]) { [TSMessage showNotificationWithTitle:@"未开启定位服务" subtitle:@"请开启定位服务定位您所在城市." type:TSMessageNotificationTypeError]; } // 2 else if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) { [self.locationManager requestWhenInUseAuthorization]; [self.locationManager startUpdatingLocation]; } // 3 else { [self.locationManager requestAlwaysAuthorization]; [self.locationManager startUpdatingLocation]; } } - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations { if (self.isFirstUpdate) { // 4 self.isFirstUpdate = NO; return; } // 5 CLLocation *newLocation = [locations lastObject]; self.currentLocation = newLocation; CLGeocoder *geocoder = [[CLGeocoder alloc] init]; // 反向地理编译出地址信息 [geocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) { if (! error) { if ([placemarks count] > 0) { CLPlacemark *placemark = [placemarks firstObject]; // 获取城市 NSString *city = placemark.locality; if (! city) { // 6 city = placemark.administrativeArea; } self.currentCity = city; } else if ([placemarks count] == 0) { [TSMessage showNotificationWithTitle:@"GPS故障" subtitle:@"定位城市失败" type:TSMessageNotificationTypeError]; } } else { [TSMessage showNotificationWithTitle:@"网络错误" subtitle:@"请检查您的网络" type:TSMessageNotificationTypeError]; } }]; [self.locationManager stopUpdatingLocation]; }
1、未开启定位服务
2、使用时定位
3、始终定位
4、第一次数据可以是久值,需舍弃
5、locations中有两个元素,第一个为旧值,第二个为新值
6、四大直辖市的城市信息无法通过locality获得,只能通过获取省份的方法来获得(如果city为空,则可知为直辖市)
最后通关副本:
参考博客:
/article/10463720.html
http://blog.it985.com/13173.html
Done
作者 @biggergao2016年05月15日
相关文章推荐
- iOS开发——响应链(Responder Chain)的深入理解和代码示例
- iOS开发--关于TableViewCell的可视化设置细节
- iOS-Runtime知识点整理,ios-runtime知识点
- iOS中书写代码规范
- iOS开发-解决AVAudioRecorder录音文件无法保存的问题
- iOS-队列.线程.任务----转自 第七城市
- iOS图形绘制CoreGraphics篇
- IOS博客项目搭建-15-发微博-自定义键盘上的Toolbar
- IOS博客项目搭建-将项目推送到Github进行管理
- iOS7.1企业应用"无法安装应用程序 因为证书无效"的解决方案
- iOS 开发常见问题
- iOS开发学习线路图
- iOS Quartz2D基本图形绘制(一)
- iOS开发——SDWebImage框架使用详解
- iOS 高效添加圆角效果实战讲解
- ios runtime swizzle
- iOS----POST异步请求解析数据时bug解决办法
- iOS小知识汇总
- 如何退出当前线程及线程安全
- iOS开发入门