We need CoreLocation Framework, so first we include CoreLocation Framework in our application.
ViewController.h
#import <CoreLocation/CoreLocation.h>
@interface ViewController : UIViewController<NSCLLocationManagerDelegate>
@property(nonatomic,retain)NSString *strLocation;
ViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
CLLocationManager *location=[[CLLocationManager alloc]init];
location.delegate=self;
location.desiredAccuracy=kCLLocationAccuracyNearestTenMeters;
[location startUpdatingLocation];
//
// Do any additional setup
after loading the view, typically from a nib.
}
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
CLGeocoder *geoCoder=[[CLGeocoder alloc]init];
[geoCoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error) {
// for(CLPlacemark *placemark
in placemarks)
// {
CLPlacemark *placeMark=[placemarks objectAtIndex:0];
if(flag == 0)
{
self.strLocation=placeMark.locality;
[self loadEvent:self.strLocation];
flag=1;
}
// }
}];
//
[self.activityIndicator stopAnimating];
}
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"Error occur %@,%@",manager,error);
}