.m
#import <CoreLocation/CoreLocation.h>
@interface HomeViewController ()<CLLocationManagerDelegate>{
NSString* currentCity;//當前城市
}
@property (strong, nonatomic) CLLocationManager* locationManager;?
- (void)viewDidLoad {
? ? [super viewDidLoad];
? ? // Do any additional setup after loading the view.
? ? [self startLocation];
}
#pragma mark---------判斷定位操作是否被允許-----------
-(void)startLocation{
if ([CLLocationManager locationServicesEnabled]) {//判斷定位操作是否被允許
? ? ? ? self.locationManager= [[CLLocationManager alloc]init];
? ? ? ? self.locationManager.delegate = self;//遵循代理
? ? ? ? self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
? ? ? ? self.locationManager.distanceFilter = 10.0f;
? ? ? ? [_locationManager requestWhenInUseAuthorization];//使用程序其間允許訪問位置數據(iOS8以上版本定位需要)
? ? ? ? [self.locationManager startUpdatingLocation];//開始定位
? ? }else{//不能定位用戶的位置的情況再次進行判斷,并給與用戶提示
? ? ? ? //1.提醒用戶檢查當前的網絡狀況
? ? ? ? //2.提醒用戶打開定位開關
? ? ? ? ? ? UIAlertController* alertVC = [UIAlertControlleralertControllerWithTitle:@"允許\"定位\"提示"message:@"請在設置中打開定位"preferredStyle:UIAlertControllerStyleAlert];
? ? ? ? ? ? UIAlertAction* ok = [UIAlertActionactionWithTitle:@"打開定位"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction*_Nonnullaction) {
? ? ? ? ? ? ? ? //打開定位設置
? ? ? ? ? ? ? ? NSURL*settingsURL = [NSURLURLWithString:UIApplicationOpenSettingsURLString];
? ? ? ? ? ? ? ? [[UIApplicationsharedApplication]openURL:settingsURL];
? ? ? ? ? ? }];
? ? ? ? ? ? UIAlertAction * cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
? ? ? ? ? ? }];
? ? ? ? ? ? [alertVCaddAction:cancel];
? ? ? ? ? ? [alertVCaddAction:ok];
? ? ? ? ? ? [self presentViewController:alertVC animated:YES completion:nil];
? ? }
}
#pragma mark---------定位成功---------
- (void)locationManager:(CLLocationManager*)manager didUpdateLocations:(NSArray*)locations{
? ? //當前所在城市的坐標值
? ? CLLocation*currLocation = [locations lastObject];
?? // NSLog(@"當前經度=%f 當前緯度=%f 當前高度=%f", currLocation.coordinate.latitude, currLocation.coordinate.longitude, currLocation.altitude);
? ? //根據經緯度反向地理編譯出地址信息
? ? CLGeocoder* geoCoder = [[CLGeocoderalloc]init];
? ? [geoCoderreverseGeocodeLocation:currLocationcompletionHandler:^(NSArray*placemarks,NSError*error) {
? ? ? ? ? ? ? ? if(placemarks.count>0) {
? ? ? ? ? ? ? ? ? ? CLPlacemark*placeMark = placemarks[0];
? ? ? ? ? ? ? ? ? ? currentCity= placeMark.locality;
? ? ? ? ? ? ? ? ? ? if(!currentCity) {
? ? ? ? ? ? ? ? ? ? ? ? currentCity=@"無法定位當前城市";
? ? ? ? ? ? ? ? ? ? }
?? ? ? ? ? ? ? ? ? // NSLog(@"%@",currentCity); //這就是當前的城市
? ? ? ? ? ? ? ? ?// ? NSLog(@"%@",placeMark.name);//具體地址:? xx市xx區(qū)xx街道
? ? ? ? ? ? ? ? //
//? ? ? ? for (CLPlacemark * placemark in placemarks) {
//
//? ? ? ? ? ? NSDictionary *address = [placemark addressDictionary];
//
//? ? ? ? ? ? //? Country(國家)? State(省)? City(市)
//? ? ? ? ? ? NSLog(@"#####%@",address);//所有返回屬性
//
//? ? ? ? ? ? NSLog(@"%@", [address objectForKey:@"Country"]);
//
//? ? ? ? ? ? NSLog(@"%@", [address objectForKey:@"State"]);
//
//? ? ? ? ? ? NSLog(@"%@", [address objectForKey:@"City"]);
//
//? ? ? ? }
? ? }];
}
#pragma mark---------定位失敗-----------
-(void)locationManager:(CLLocationManager*)manager didFailWithError:(NSError*)error{
? ? if([errorcode] ==kCLErrorDenied){
? ? ? ? //訪問被拒絕
? ? ? ? ALERT(@"提示", @"位置訪問被拒絕");
? ? }
? ? if ([error code] == kCLErrorLocationUnknown) {
? ? ? ? //無法獲取位置信息
? ? ? ? ALERT(@"提示", @"無法獲取位置信息");
? ? }
}