iOS:CoreLocation實現定位當前城市

首先導入頭文件

 

#import <CoreLocation/CoreLocation.h>

在info.plist文件中添加:

注:NSLocationAlwaysUsageDescription可以不添加

下面是具體用法的demo:

注意:
1.[CLLocationManager locationServicesEnabled]判斷定位是否開啓是判斷的整個手機系統的定位是否打開,並不是針對這一應用.
2.具體在本應用中的是否已經授權定位要通過代理方法判斷
如果定位失敗(未授權)則會執行此代理方法

 

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {

}

具體代碼:

 

@interface ViewController ()<CLLocationManagerDelegate>
{
    CLLocationManager * locationManager;
    NSString * currentCity; //當前城市
}
@end
@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    [self locate]; 
}

- (void)locate { 
     //判斷定位功能是否打開
    if ([CLLocationManager locationServicesEnabled]) {
        locationManager = [[CLLocationManager alloc] init];
        locationManager.delegate = self;
//        [locationManager requestAlwaysAuthorization];
        currentCity = [[NSString alloc] init];
        [locationManager startUpdatingLocation];    
    }
    
}

#pragma mark CoreLocation delegate  

//定位失敗則執行此代理方法
//定位失敗彈出提示框,點擊"打開定位"按鈕,會打開系統的設置,提示打開定位服務
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
    UIAlertController * alertVC = [UIAlertController alertControllerWithTitle:@"允許\"定位\"提示" message:@"請在設置中打開定位" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction * ok = [UIAlertAction actionWithTitle:@"打開定位" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        //打開定位設置
        NSURL *settingsURL = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
        [[UIApplication sharedApplication] openURL:settingsURL];
    }];
    UIAlertAction * cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alertVC addAction:cancel];
    [alertVC addAction:ok];
    [self presentViewController:alertVC animated:YES completion:nil];
    
}
//定位成功
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
    [locationManager stopUpdatingLocation];
    CLLocation *currentLocation = [locations lastObject];
    CLGeocoder * geoCoder = [[CLGeocoder alloc] init];
    
      //反編碼
    [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(@"%@",currentCity); //這就是當前的城市
            NSLog(@"%@",placeMark.name);//具體地址:  xx市xx區xx街道
        }
        else if (error == nil && placemarks.count == 0) {
            NSLog(@"No location and error return");
        }
        else if (error) {
            NSLog(@"location error: %@ ",error);
        }
  
    }];     
}
@end

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章