- (void)viewDidLoad
{
[superviewDidLoad];
// Do any additional setup after loading the view.
// 初始化位置管理器
if (![CLLocationManagerlocationServicesEnabled])
{
UIAlertView *locationServiceAlert = [[UIAlertViewalloc]initWithTitle:nilmessage:@"定位不可用" delegate:nilcancelButtonTitle:@"我知道了" otherButtonTitles:nil];
[locationServiceAlert show];
}
else
{
locationManager = [[CLLocationManageralloc]init];
locationManager.delegate =self;
[locationManagerstartUpdatingLocation];
}
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
// 获取当前所在的城市名(地理反编码)
CLGeocoder *geocoder = [[CLGeocoderalloc]init];
[geocoder reverseGeocodeLocation:newLocationcompletionHandler:^(NSArray *array,NSError *error)
{
if (array.count >0)
{
CLPlacemark *placemark = [arrayobjectAtIndex:0];
NSString *city = placemark.administrativeArea;
NSLog(@"城市为--- %@", city);
}
else if (error ==nil && [arraycount] ==0)
{
NSLog(@"No results were returned.");
}
else if (error !=nil)
{
NSLog(@"An error occurred = %@", error);
}
}];
}