- 所需框架
#import <CoreLocation/CoreLocation.h>
- 所需代理
CLLocationManagerDelegate
- 开始定位
//开始定位
self.locationManager = [[CLLocationManager alloc] init];
[_locationManager requestAlwaysAuthorization];
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.distanceFilter = 10.0f;
[self.locationManager 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(区)
NSString * title = [test objectForKey:@"State"];
NSLog(@"%@",title);
CLLocation *loc = newLocation;
NSLog(@"纬度=%f,经度=%f",loc.coordinate.latitude,loc.coordinate.longitude);
}
}];
}
在iOS10中如果获取用户位置信息,必须要在info.plist文件中进行授权声明,否则会导致crash