#import <CoreLocation/CoreLocation.h> 使用到的头文件 要引入CoreLocation这个包<CLLocationManagerDelegate> 使用的代理名称//1.使用定位服务 //设置app有访问定位服务的权限 //在使用应用期间 / 始终(app在后台) //info.plist文件添加以下两条(或者其中一条): //NSLocationWhenInUseUsageDescription 在使用应用期间 //NSLocationAlwaysUsageDescription 始终 //2.LocationManager 对象管理相关的定位服务 _manager = [[CLLocationManager alloc] init]; //manager判断: 手机是否开启定位 / app是否有访问定位的权限 //[CLLocationManager locationServicesEnabled]; //手机是否开启定位 //[CLLocationManager authorizationStatus]; //app访问定位的权限的状态 if (![CLLocationManager locationServicesEnabled] || [CLLocationManager authorizationStatus] != kCLAuthorizationStatusAuthorizedWhenInUse) { [_manager requestWhenInUseAuthorization]; //向用户请求访问定位服务的权限 } _manager.delegate = self; _manager.desiredAccuracy = kCLLocationAccuracyBest; _manager.distanceFilter = 1.0f; [_manager startUpdatingLocation];//定位代理经纬度回调-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { [_manager stopUpdatingLocation]; CLGeocoder * geoCoder = [[CLGeocoder alloc] init]; [geoCoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error) { for (CLPlacemark * placemark in placemarks) { NSDictionary *test = [placemark addressDictionary]; // Country(国家) State(城市) SubLocality(区) Name全称 NSLog(@"%@", [test objectForKey:@"Name"]); } }];}
新闻热点
疑难解答