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
原创粉丝点击