定位的基本使用
来源:互联网 发布:ubuntu使用数据库 编辑:程序博客网 时间:2024/06/06 03:37
iOS在实现定位功能前首先需要做两件事:
1、导入主头文件
#import <CoreLocation/CoreLocation.h>
2、在info.plist 文件中添加权限<key>NSLocationAlwaysUsageDescription</key> <string>App需要您的同意,才能始终访问位置</string>
实现代码:
/*
iOS8以前:系统会自动申请用户位置权限
iOS8之后(包含iOS8.0): 必须要程序员去申请用户的位置权限
*/
#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>
@interface ViewController ()<CLLocationManagerDelegate>
@property (nonatomic,strong) CLLocationManager *locationManager;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 位置管理器常见属性
// 当用户移动了多长的距离之后,才更新用户位置,单位(米)
self.locationManager.distanceFilter = 10;
// 定位精度,精度越高越费电
self.locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
// extern const CLLocationAccuracy kCLLocationAccuracyBestForNavigation; 导航使用最好进度
// extern const CLLocationAccuracy kCLLocationAccuracyBest; 最高精度
// extern const CLLocationAccuracy kCLLocationAccuracyNearestTenMeters; 10M
// extern const CLLocationAccuracy kCLLocationAccuracyHundredMeters; 100M
// extern const CLLocationAccuracy kCLLocationAccuracyKilometer; 1000M
// extern const CLLocationAccuracy kCLLocationAccuracyThreeKilometers; 3000M
self.locationManager.activityType = CLActivityTypeFitness;
// CLActivityTypeOther = 1,
// CLActivityTypeAutomotiveNavigation, // 汽车导航
// CLActivityTypeFitness, // 步行导航
// CLActivityTypeOtherNavigation // 其他导航,比如轮船,火车,飞机
//
if ([UIDevice currentDevice].systemVersion.doubleValue >= 8.0) {
// 申请用户位置权限
// 无论应用在前台还是在后台都需要使用用户的位置信息
[self.locationManager requestAlwaysAuthorization];
}else{
//开始更新用户位置
[self.locationManager startUpdatingLocation];
}
}
#pragma mark - CLLocationManagerDelegate
//当授权状态发生改变了就调用该代理方法
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
// 当前请求到用户的权限后开始定位
if (status == kCLAuthorizationStatusAuthorizedAlways) {
[self.locationManager startUpdatingLocation];
}else{
NSLog(@"用户授权失败");
}
}
//当更新用户位置的时候执行
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations
{
//距离当前时间最近的那个位置是这个数组的最后一个元素
CLLocation *location = locations.lastObject;
//地理编码器
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
//反地理编码
[geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
//CLPlacemark 地标: 描述了地理的经纬度,已经地名,省,市,区,街道
for (CLPlacemark *placemark in placemarks) {
NSDictionary *addressDic = placemark.addressDictionary;
NSString *state=[addressDic objectForKey:@"State"];
NSString *city=[addressDic objectForKey:@"City"];
NSString *subLocality=[addressDic objectForKey:@"SubLocality"];
NSString *street=[addressDic objectForKey:@"Street"];
NSLog(@"位置 = %@ %@ %@ %@", state, city, subLocality, street);
}
}];
}
/// 懒加载
- (CLLocationManager *)locationManager
{
if (_locationManager == nil) {
_locationManager = [[CLLocationManager alloc] init];
_locationManager.delegate = self;
}
return _locationManager;
}
@end
在XCode中打印出:
- 定位的基本使用
- 定位CoreLocation的基本使用
- 百度Map的基本使用和定位
- IOS定位基本使用
- swift地图定位(十二)大头针的基本使用
- iOS 地图定位及大头针的基本使用
- 定位的使用实例
- 地图的使用(定位)
- 定位的配合使用
- CSS定位的使用
- ios百度地图的使用(基本定位和地理编码)
- 菜鸟学习之路—— CoreLocation(定位功能)的基本使用
- ios百度地图的使用(基本定位和地理编码)
- 使用百度地图实现基本的地图显示与定位功能
- Android自动化-使用interface实现by定位、元素基本操作
- 定位,画布,BFC的使用
- Android 定位GPS的使用
- 百度地图定位的使用
- ckeditor的使用(行距,上传图片,字体)
- 机器学习第一篇——python开发环境的搭建
- git
- session有效期问题
- 欢迎使用CSDN-markdown编辑器
- 定位的基本使用
- NGUI UIcenterOnChild
- android开发之&android中的swift,kotlin基础语法(一)
- android 基础框架依赖库
- The basic concepts of OFDM
- 树的重心 Shaass the Great
- 《STL源码剖析》STL空间配置器
- 自适应表单页面对齐方案
- 【Python学习系列十一】Python实现决策树实现C4.5(信息增益率)