Automatically obtain the current location (GPS positioning) in iOS development)

Source: Internet
Author: User

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.


   @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


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.