[iPhone硬件]-GPS定位的使用
2016-02-01 18:47
393 查看
iPhone中GPS定位如何使用
关键词
1.info.plist配置授权描述2.引入库
3.CLLocationManager的使用
info.plist配置
在info.plist中根据情况加入以下两个string类型配置项,并填写描述
1.NSLocationAlwaysUsageDescription2.NSLocationWhenInUseUsageDescription
引入库
-引入CoreLocation.framework-在原文件中引入 <CoreLocation/CoreLocation.h>
代码实现
// 初始化 2.- (void)setupLocationManager { 3. _locationManager = [[CLLocationManager alloc] init]; 4. _locationManager.desiredAccuracy = kCLLocationAccuracyBest; 5. _locationManager.distanceFilter = kCLDistanceFilterNone; 6. 7. if ([_locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) { 8. [_locationManager requestWhenInUseAuthorization]; 9. } 10.} 11. 12.// 调用此方法开始定位 13.- (void) startUpdatingLocation { 14. if ([CLLocationManager locationServicesEnabled]) { 15. _shouldHandleLocation = YES; 16. _locationManager.delegate = self; 17. [_locationManager startUpdatingLocation]; 18. } else { 19. [SVProgressHUD showErrorWithStatus:@"请开启定位功能!"]; 20. } 21.} 22. 23.#pragma mark - CLLocationManagerDelegate 24.- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations 25.{ 26. CLLocation *newLocation = [locations lastObject]; 27. if (_shouldHandleLocation) { 28. _shouldHandleLocation = NO; 29. _latitude = newLocation.coordinate.latitude; 30. _longitude = newLocation.coordinate.longitude; 31. 32. [self commitCheckinInfo]; 33. } 34. 35. _locationManager.delegate = nil; 36. [_locationManager stopUpdatingLocation]; 37.} 38. 39.- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error 40.{ 41. [SVProgressHUD showErrorWithStatus:@"定位失败!"]; 42. _locationManager.delegate = nil; 43. [manager stopUpdatingLocation]; 44.}
相关文章推荐
- Nginx代理非80端口
- git教程收集
- 328. Odd Even Linked List
- Qt同步线程(比较清楚,而且QMutex QMutexLocker QReadWriteLock QSemaphore QWaitCondition 每个都有例子)
- SICP 习题 (2.7) 解题总结 : 定义区间数据结构
- 安卓常见反破解方式
- pyinstaller使用-python项目转换成exe可执行文件
- 【ROS学习】(七)ROS参数服务(1)
- pyinstaller使用-python项目转换成exe可执行文件
- EventBus初理解
- android中BuildConfig.DEBUG的使用
- 为满足不断增长的业务需求 升级现有的服务器架构--达达
- Java Web 三大框架开发资料
- myEclipse+tomcat 热部署
- gitlab服务安装手册
- UVA 1626 Brackets sequence(括号匹配 + 区间DP)
- EF Code First Migration总结
- java保存log到文件中
- 2016-02-01 FFC
- 腾讯应用加固的脱壳分析和修复