iOS--CLLocationManager 定位
来源:互联网 发布:淘宝 中老年男装毛衣 编辑:程序博客网 时间:2024/06/15 19:35
项目中需要使用获取用户当前位置,但是不使用第三方的SDK。所以只能使用苹果自带的CoreLocation。iOS 8出来之后,针对定位需要多一点处理,才能正常定位。
iOS 8 定位;
注意
在info.plist文件中加入两个字段
//允许在前台使用时获取GPS的描述
NSLocationAlwaysUsageDescription=YES
//允许永久使用GPS的描述
NSLocationWhenInUseUsageDescription=YES
还需要导入CoreLocation.framework框架
接下来是相关代码
#import "ViewController.h"#import <CoreTelephony/CTTelephonyNetworkInfo.h>#import <CoreTelephony/CTCarrier.h>// 定位#import <CoreLocation/CoreLocation.h>@interface ViewController ()<CLLocationManagerDelegate>{ /** 定位服务*/ CLLocationManager *locationManger; /** 当前城市*/ NSString *currentCity; /**经度*/ NSString *Strlatitude; /** 纬度*/ NSString *Strlongitude;}@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. [self getcarrierName]; // 定位 [self locatemap];}- (void)locatemap { // 判断定位功是否打开 if ([CLLocationManager locationServicesEnabled]) { locationManger = [[CLLocationManager alloc]init]; locationManger.delegate = self; [locationManger requestAlwaysAuthorization]; currentCity = [[NSString alloc]init]; [locationManger requestWhenInUseAuthorization]; // 设置寻址经度 locationManger.desiredAccuracy = kCLLocationAccuracyBest; locationManger.distanceFilter = 5.0; [locationManger startUpdatingLocation]; }}#pragma mark -- CoreLocation delegate (定位失败)// 定位失败则执行代理方法// 定位失败弹出提示框.点击"打开定位"- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error { // 设置提示提醒用户打开定位服务 UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"允许\"定位\"提示" message:@"请在设置中打开定位" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *ok = [UIAlertAction actionWithTitle:@"打开定位" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { // 打开定位设置 NSURL *settingUrl = [NSURL URLWithString:UIApplicationOpenSettingsURLString]; [[UIApplication sharedApplication]openURL:settingUrl]; }]; UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { }]; [alert addAction:cancel]; [alert addAction:ok]; [self presentViewController:alert animated:YES completion:nil];}#pragma mark -- 定位成功- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{ [locationManger stopUpdatingHeading]; // 旧值 CLLocation *currentLocation = [locations lastObject]; CLGeocoder *geoCoder = [[CLGeocoder alloc]init]; // 打印当前经纬度 NSLog(@"%f==%f", currentLocation.coordinate.latitude, currentLocation.coordinate.longitude); // 地理反编码 可以根据地理坐标确定地理位置信息 (街道门派) [geoCoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) { if (placemarks.count > 0) { CLPlacemark *placeMark = placemarks[0]; currentCity = placeMark.locality; if (!currentCity) { currentCity = @"无法定位当前城市"; } NSLog(@"当前国家==%@", placeMark.country); NSLog(@"当前城市==%@", currentCity); NSLog(@"当前位置==%@", placeMark.subLocality); NSLog(@"当前街道==%@", placeMark.thoroughfare); NSLog(@"当前具体位置==%@", placeMark.name); } else if (error == nil && placemarks.count == 0){ NSLog(@"no location and error return"); } else if (error) { NSLog(@"location error %@", error); } }];}@end
0 0
- ios定位CLLocationManager
- ios CLLocationManager定位
- iOS CLLocationManager定位
- iOS CLLocationManager定位
- iOS CLLocationManager定位
- iOS--CLLocationManager 定位
- iOS CLLocationManager定位,IOS8注意
- Ios开发之定位CLLocationManager
- iOS开发之定位CLLocationManager
- IOS Swift3 使用 CLLocationManager 定位
- IOS:ios8.0下CLLocationManager定位服务
- IOS CLLocationManager 设置定位请求的方式
- 定位 (CLLocationManager )
- CLLocationManager 定位
- 定位CLLocationManager
- 定位 CLLocationManager
- IOS开发教程--Xcode6,CLLocationManager无法定位解决方案
- 关于IOS定位和CLLocationManager 小知识点总结
- 常用的对数组的操作
- 【初】Eclipse构建maven web项目
- HBase 实战中遇到的坑
- GitHub网页的功能
- Hadoop Mapper 示例
- iOS--CLLocationManager 定位
- maven语法详解pom
- Git教程及问题解析
- Codeforces Round #392 (Div. 2)-D. Ability To Convert(贪心+dp)
- github-ranking
- 【Leetcode】171. Excel Sheet Column Number
- linux 在线调试摄像头驱动
- 年度 | 2016 IBM 大数据科技盘点
- 线性表顺序存储设计与实现_api函数实现