CoreLocation tutorial in iphone development

Implementing Built-in GPS in your app, it gives more advantage to you and users feel more comfortable with your apps, in ios development implementing this ,is very simple . Apple has powerful framework to do this,namely CoreLocation frameWork.But one thing is if you didnt properly use this methods,it would consume more power, so you have to do it properly,shutdown it as procedure,by doing this CoreLocation framework would consume less power, now we have look at this framework.

Since we’re going to be using Core Location with our iOS app, we need to add the header file (CoreLocation/CoreLocation.h) to our prefix header (usually with the extension .pch). In this prefix header, locate the #ifdef __OBJC__ line,

#ifdef __OBJC__

    #import

    #import

    #import  // added for Core Location

#endif

 

Next step is add CoreLocation framework in your project.by right click on frameworks->add existing framework->CoreLocation frameWork,

 

this framework has some important delegate methods,

 

We need to implement two important delegates in our class where we want to get messages ,

 

(void)locationManager🙁CLLocationManager *)manager didUpdateToLocation🙁CLLocation *)newLocation fromLocation🙁CLLocation *)oldLocation

 

this method will be called when new location messages recieved..

 

(void)locationManager🙁CLLocationManager *)manager didFailWithError🙁NSError *)error

 

This Delegate will be invoked if corelocation needs to report us failure messages,

 

Next we need to store the data where we currently located, for this add these two lines in @interface definition file.

 

@property (strong, nonatomic) CLLocationManager *locationManager;

@property (strong, nonatomic) CLLocation *currentLocation;

 

and add this line in @implementation file

 

@synthesize locationManager, currentLocation;

 

 

IMPLEMENTATION :

 

(void)locationManager🙁CLLocationManager *)manager didUpdateToLocation🙁CLLocation *)newLocation fromLocation🙁CLLocation *)oldLocation {

    self.currentLocation = newLocation;

 

    if(newLocation.horizontalAccuracy <= 100.0f) 

    {

         [locationManager stopUpdatingLocation]

    }

}

 

 

– (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {

    if(error.code == kCLErrorDenied) {

        [locationManager stopUpdatingLocation];

    } else if(error.code == kCLErrorLocationUnknown) {

        // retry

    } else {

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@”Error retrieving location”

                              message:[error description]

                              delegate:nil

                              cancelButtonTitle:@”OK”

                              otherButtonTitles:nil];

        [alert show];

    }

}

 

REQUESTING UPDATES:

 

to setup corelocation you need to add these two line.

 

locationManager = [[CLLocationManager alloc] init];

locationManager.delegate = self;

[locationManager startUpdatingLocation];

 

Thats it,

I hope that this article might be useful, post your reviews and feedback below here to improve my skills.

Comments

comments

Leave a Reply

Your email address will not be published. Required fields are marked *