Location TutorialDecember 06, 2010 11:35 am Posted By: bytedissident
This is a simple tutorial designed to get you moving on using Apple’s Location services in your iOS applications. Below is a quick step by step that should get you up and running quickly. Before we start create a View Based Application in XCode called LocationTutorial.
Step 1:
First of all we need to make sure we add the Core Location framework to our project. To do this ctrl-click or right click on your project Icon and then your application’s target. Then in the right panel choose “Build Phases”

Then open “Link With Binary Libraries” and press the + symbol. Scroll down the list of frameworks. Then highlight Core Location and click “Add”

Step 2:
In the LocationTutorialAppDelegate.h file of your project import this frame work.

Step 3:
In your header file you will need to make you sure you add the CLLocationManagerDelegate so that Core Location can return you info as it comes in.
@interface LocationTutorialAppDelegate : NSObject {
UIWindow *window;
LocationTutorialViewController *viewController;
CLLocationManager *locationManager;
}
@property(nonatomic,retain)CLLocationManager *locationManager;
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet LocationTutorialViewController *viewController;
Step 4:
Now in your header file create a property for your CLLocationManager and in your delegates implementation file synthesize your property;
@implementation LocationTutorialAppDelegate
@synthesize window;
@synthesize viewController;
@synthesize locationManager;
Step 5:
Now that your set up is complete you will just need to instantiate the class, set the delegate to self and call the StartUpdatingLocation method.
self.locationManager = [[[CLLocationManager alloc] init] autorelease];
self.locationManager.delegate = self;
Here we can can optionally set the accuracy property
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
Here we start the service
[self.locationManager startUpdatingLocation];
Other accuracy properties:
kCLLocationAccuracyBestForNavigation;
kCLLocationAccuracyBest;
kCLLocationAccuracyNearestTenMeters;
kCLLocationAccuracyHundredMeters;
kCLLocationAccuracyKilometer;
kCLLocationAccuracyThreeKilometers;
Note: Do not forget to release your property in your dealloc method;
Step 6:
Finally you will need to include Core Location’s delegate locationManager method to get the return values from the framework. You’ll notice here that I am converting the newLocation coordinates to strings. It’s up to you to decide what you want to do with your new data.
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {
NSLog(@"%@",[NSString stringWithFormat:@"%3.5f", newLocation.coordinate.latitude]);
NSLog(@"%@",[NSString stringWithFormat:@"%3.5f", newLocation.coordinate.longitude]);
}
Now you will probably want to experiment with a number of things. Just to point you in the right direction take a look at this distance method
CLLocationDistance distance = [newLocation distanceFromLocation:oldLocation];
Download Code
|