- #import <CoreLocation/CoreLocation.h>
- @interface PBTextViewController ()<CLLocationManagerDelegate>
- @property (weak, nonatomic) IBOutlet UIImageView *bkImageView;
- _locationManager = [[CLLocationManager alloc] init];
- _locationManager.delegate = self;
- _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
- _locationManager.distanceFilter = 1000.0f;
- #pragma mark---- location
- - (void)startUplocation
- {
- if ([CLLocationManager locationServicesEnabled])
- {
- [_locationManager startUpdatingLocation];
- }
- else
- {
- UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请开启定位功能!"delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil, nil nil];
- [av show];
- }
- }
- #pragma mark - CLLocationManagerDelegate
- // 地理位置发生改变时触发
- - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
- {
- // 获取经纬度
- NSLog(@"纬度:%f",newLocation.coordinate.latitude);
- NSLog(@"经度:%f",newLocation.coordinate.longitude);
- // 停止位置更新
- [manager stopUpdatingLocation];
- CLGeocoder* geocoder = [[CLGeocoder alloc] init];
- [geocoder reverseGeocodeLocation: newLocation completionHandler:^(NSArray *array, NSError *error) {
- if (array.count > 0)
- {
- CLPlacemark *placemark = [array objectAtIndex:0];
- NSString *city = placemark.locality;
- NSString *street = placemark.thoroughfare;
- NSLog(@"%@,%@",city,street);
- NSMutableString *ms = [[NSMutableString alloc] init];
- [ms appendString:city];
- [ms appendString:street];
- _locationLb.text = ms;
- }
- }];
- }
- // 定位失误时触发
- - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
- {
- NSLog(@"error:%@",error);
- }