Automatically obtain the current location (GPS positioning) in iOS development)
Development Environment xcode5.0
First, we need to introduce this framework CoreLocation. framework
Import this library # import
And his proxy method CLLocationManagerDelegate
GPSViewController. h
Note that the CLLocationManager * locationmanager must be set to a global variable. Otherwise, you will not be able to get the desired result! Why? I still don't know.
#import
#import
@interface GPSViewController : UIViewController
@property(nonatomic,retain) CLLocationManager* locationmanager;@end
GPSViewController. m
# Import GPSViewController. h @ interface GPSViewController () @ end @ implementation GPSViewController @ synthesize locationmanager;-(id) initWithNibName :( NSString *) bundle :( NSBundle *) Detail {self = [super initWithNibName: nibNameOrNil bundle: nibBundleOrNil]; if (self) {// Custom initialization} return self;}-(void) viewDidLoad {[super viewDidLoad]; // Do any additional setup after loading the view. locationmanager = [[CLLocationManager alloc] init]; // set the precision/* kCLLocationAccuracyBest precise locating accuracy * // set the positioning precision [locationmanager setDesiredAccuracy: kCLLocationAccuracyBest]; // implement the locationmanager protocol. delegate = self; NSLog (@ start locating); // start locating [locationmanager startUpdatingLocation];}
# Pragma mark locationManager delegate-(void) locationManager :( CLLocationManager *) manager didUpdateToLocation :( CLLocation *) newLocation fromLocation :( CLLocation *) oldLocation {NSLog (@ hello ); // print the accuracy and latitude CLLocationCoordinate2D coordinate = newLocation. coordinate; NSLog (@ output current precision and latitude); NSLog (@ precision: % f latitude: % f, coordinate. latitude, coordinate. longpolling); // stop locating [locationmanager stopUpdatingLocation]; // calculate the float distance = [newLocation distanceFromLocation: oldLocation]; NSLog (@ distance % f, distance );} @ end