首先要导入CoreLocation.framework这个框架
然后在AppDelegate的头文件,#import<CoreLocation/CoreLocation.h>
实现CLLocationManagerDelegate这个接口
然后在AppDelegate的实现类的事件中如:- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions方法里写:
CLLocationManager *_locManager = [[CLLocationManageralloc]init];
[_locManager setDelegate:self];
[_locManager setDesiredAccuracy:kCLLocationAccuracyBest];
[_locManager startUpdatingLocation];
然后再实现delegate
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
CLLocationCoordinate2D loc = [newLocation coordinate];
NSString *lat =[NSStringstringWithFormat:@"%f",loc.latitude];//get latitude
NSString *lon =[NSStringstringWithFormat:@"%f",loc.longitude];//get longitude
NSLog(@"%@ %@",lat,lon);
}