iOS-定位
来源:互联网 发布:淘宝4.2 编辑:程序博客网 时间:2024/05/17 01:22
使用地图的CLLocationManager完成定位效果.
例子:定位当前所在位置,把所在城市的城市名显示在label上
实现定位效果步骤如下:
1.导入头文件
#import <CoreLocation/CoreLocation.h>
2.创建位置管理者,和 CLGeocoder 类的对象.这里把他设置成属性
/** 位置管理者*/@property (nonatomic,strong) CLLocationManager *LManager ;/** 将经度和纬度解析成地名*/@property (nonatomic,strong) CLGeocoder *geocoder;
使用懒加载初始化
//懒加载- (CLLocationManager *)LManager{ if (!_LManager) { //创建位置管理者 _LManager = [[CLLocationManager alloc] init]; _LManager.delegate = self; } return _LManager;}- (CLGeocoder *)geocoder{ if (!_geocoder) { _geocoder = [[CLGeocoder alloc] init]; } return _geocoder;}
在这里需要知道,定位在进入iOS8之后,需要修改plist文件,添加两个字段,分别是 NSLocationAlwaysUsageDescription 以及 NSLocationWhenInUseDescription 如下图所示
由于只有定位需求,所以在viewDidLoad方法里面直接写定位方法.
首先判断用户是否关闭了定位
//判断用户是否关闭了定位 if (![CLLocationManager locationServicesEnabled]) { NSLog(@"定位失败"); return; }
版本适配
//做版本适配(ios8以后才有的方法,所以要做适配) if ([self.LManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) { [self.LManager requestWhenInUseAuthorization]; }
使用位置管理者,开始更新用户位置
//使用位置管理者,开始更新用户位置 [self.LManager startUpdatingLocation]; //iOS9.0以后新的请求定位方法,不能与startUpdatingLocation同时使用 // [self.LManager requestLocation];
让当前类遵从CLLocationManagerDelegate协议,并实现协议里面的一些方法
CLLocationManagerDelegate
定位失败时调用
/** 定位失败时调用*/- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{ NSLog(@"定位失败");}
位置更新后调用
/** 位置更新后调用*/- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{ CLLocation *location = [locations lastObject]; [self.geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) { for (CLPlacemark *placemark in placemarks) { for (NSString *key in placemark.addressDictionary) { NSLog(@"%@ = %@",key,placemark.addressDictionary[key]); [self.label performSelectorOnMainThread:@selector(setText:) withObject:placemark.addressDictionary[@"City"] waitUntilDone:YES]; } } }];}
定位服务状态改变时调用
/** 定位服务状态改变时调用*/- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{ switch (status) { case kCLAuthorizationStatusNotDetermined: { NSLog(@"用户还未决定授权"); if ([manager respondsToSelector:@selector(requestWhenInUseAuthorization)]) { [manager requestAlwaysAuthorization]; } break; } case kCLAuthorizationStatusRestricted: { NSLog(@"访问受限"); break; } case kCLAuthorizationStatusDenied: { if ([CLLocationManager locationServicesEnabled]) { NSLog(@"定位服务开启,被拒绝"); } else{ NSLog(@"定位服务关闭,不可用"); } break; } case kCLAuthorizationStatusAuthorizedAlways: { NSLog(@"获得前后台授权"); break; } case kCLAuthorizationStatusAuthorizedWhenInUse: { NSLog(@"获得前后台授权"); break; } }}
0 0
- IOS定位
- IOS定位
- IOS 定位
- iOS定位
- ios 定位
- IOS 定位
- iOS 定位
- ios定位
- iOS定位
- iOS 定位
- ios 定位
- IOS定位
- iOS 定位
- ios定位
- iOS-定位
- iOS 定位
- 【读书笔记】iOS-iOS定位
- IOS 定位系统实例
- 获取Class实例的四种方法
- HDU2222 Keywords Search [AC自动机模板]
- 基于训练数据的全局优化水下畸变图像复原
- Error creating bean with name 'amqpTemple' Could not resolve matching constructor (hint: specify
- Hibernate.cfg.xml
- iOS-定位
- String
- 杭电OJ 1032 给定i,j未说大小,两者大小不定
- C#文件流
- 逻辑地址与物理地址的转换
- 【android】:android开发积累3之如何实现菜单栏
- 简单的搭建wordpress
- JNI中很有用的jstring 与 const char* 的相互转换函数
- java第一课(1):类和对象基本篇