#import <CoreLocation/CoreLocation.h>
准守<CLLocationManagerDelegate>
@property (weak, nonatomic) IBOutlet UILabel *cityLable;
@property(nonatomic,retain)CLLocationManager* locationManage;
@property(nonatomic,copy)NSString* cityName;
@implementation ViewController
#warning 如果不成功需要在Info文件中加入两个Key
/*
NSLocationAlwaysUsageDescription
NSLocationWhenInUseUsageDescription
不需要给键赋值
*/
- (void)viewDidLoad {
[super viewDidLoad];
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[self locate];
}
-(void)locate{
//判断定位炒作是否被允许
//8.0之前的不需要判断
if ([[UIDevice currentDevice].systemVersion doubleValue]>=8.0) {
// self.cityLable.text = @"版本问题";
[self.locationManage requestWhenInUseAuthorization];
}
if ([CLLocationManager locationServicesEnabled]) {
self.locationManage = [[CLLocationManager alloc] init];
//精确的经纬度
self.locationManage.desiredAccuracy = kCLLocationAccuracyBest;
//精确的距离范围
self.locationManage.distanceFilter = 10;
self.locationManage.delegate = self;
}else{
//提示不成功的..没有打开定位开关
UIAlertController* alertCV = [UIAlertController alertControllerWithTitle:@"提示" message:@"定位不成功" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
[alertCV addAction:action];
}
//打开定位控制器
[self.locationManage startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
//location是定位的一些位置
//创建定位,当前的定位 取出最后一个 也就是现在的位置
CLLocation * currentLocation = [locations lastObject];
//反地址编码 经纬度开确定当前的经纬度
CLGeocoder* geocoader = [[CLGeocoder alloc] init];
//通过经纬度来确定城市等位置
[geocoader reverseGeocodeLocation:currentLocation completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
//存在定位的地址时判断
if (placemarks.count > 0 ) {
//获取详细的地址
CLPlacemark* placemark = [placemarks objectAtIndex:0];
// NSLog(@"%@",placemark.name);
//城市
NSString* city = placemark.locality;
if (!city) {
//如果不是城市 就可能是直辖市 就是这个
city = placemark.administrativeArea;
}
//返回 值
self.cityLable.text= city;
//如果不存在错误 且没有搜索到城市的判断
}else if (error == nil && placemarks.count == 0){
//弹框提示
UIAlertController* alertCV = [UIAlertController alertControllerWithTitle:@"提示" message:@"没有定位到城市" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
[alertCV addAction:action];
}else if(error != nil ){
//存在错误的是后也给个提示
UIAlertController* alertCV = [UIAlertController alertControllerWithTitle:@"提示" message:(NSString *)error preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
[alertCV addAction:action];
}
}];
//关闭定位 如果不关闭美国一分钟就会自动定位一次
[manager stopUpdatingLocation];
}
//定位失败的时候回调用这个方法
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
if (error.code == kCLErrorDenied) {
UIAlertController* alertCV = [UIAlertController alertControllerWithTitle:@"提示" message:@"是否是没有打开定位" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
[alertCV addAction:action];
}
} |