您的位置:首页 > 其它

通过系统定位获取位置信息

2016-09-19 14:13 453 查看
所需框架

#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)location
4000
Manager:(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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  定位