The other day I was just getting into using CoreLocation on iPhone OS, and I found a great tutorial involving making a driver class to add a level of abstraction between CLLocationManager and your code. CLLocationManager is kind of complex to a first time user, and my class simplifies it a bit. You simply implement the protocol with two callbacks (location updated, error reporting), set your interface as a delegate for my driver, and let the events start flowing. N.B: Requires your app to reference the CoreLocation framework.

WLLocationDriver.h:

//
//  WLLocationDriver.h
//
//  Created by William LaFrance on 2/23/10.
//  Public Domain
//

/**
 * Protocol for any interface being used as a delegate to WLLocationDriver.
 * This greatly simplifies using a CLLocationManager.
 */
@protocol WLLocationDriverDelegate
    @required
    - (void)locationUpdate:(CLLocation *)location;
    - (void)locationError:(NSError *)error;
@end

/**
 * A simple location driver. Excecutes two simple events: one for location
 * changes, and one for errors.
 *
 * Usage:
    locationDriver = [[WLLocationDriver alloc] init];
    locationDriver.delegate = self;
 */
@interface WLLocationDriver : NSObject  {
    CLLocationManager *locationManager;
    id delegate;
}

/**
 * The CLLocationManager we're getting events from
 */
@property (nonatomic, retain) CLLocationManager *locationManager;

/**
 * The delegate we're sending our own events to
 */
@property (nonatomic, assign) id delegate;

/**
 * Called by the location manager every time a location update occurs (ie, a new
 * GPS fix).
 */
- (void)locationManager:(CLLocationManager *)manager
        didUpdateToLocation:(CLLocation *)newLocation
        fromLocation:(CLLocation *)oldLocation;

/**
 * Called by the location manager every time a location update error occurs,
 * such as not being able to get a GPS fix.
 */
- (void)locationManager:(CLLocationManager *)manager
        didFailWithError:(NSError *)error;

/**
 * Easy access to the devices current location
 */
- (CLLocation *)getCurrentLocation;

@end

WLLocationDriver.m:

//
//  WLLocationDriver.m
//
//  Created by William LaFrance on 2/23/10.
//  Public Domain
//

#import "WLLocationDriver.h"

/**
 * A simple location driver. Excecutes two simple events: one for location
 * changes, and one for errors.
 *
 * Usage:
    locationDriver = [[WLLocationDriver alloc] init];
    locationDriver.delegate = self;
 */
@implementation WLLocationDriver

@synthesize locationManager;
@synthesize delegate;

/**
 * Initializes the location driver, constructs the location manager, and begins
 * to get event updates.
 */
- (id) init {
    self = [super init];

    if (self != nil) {
        self.locationManager = [[[CLLocationManager alloc] init] autorelease];
        self.locationManager.delegate = self;
        [self.locationManager startUpdatingLocation];
    }

    return self;
}

/**
 * Called by the location manager every time a location update occurs (ie, a new
 * GPS fix).
 */
- (void)locationManager:(CLLocationManager *)manager
        didUpdateToLocation:(CLLocation *)newLocation
        fromLocation:(CLLocation *)oldLocation
{
    if (delegate != nil)
        [self.delegate locationUpdate:newLocation];
    else
        NSLog(@"%@", @"Failed to report location -- missing delegate.");
}

/**
 * Called by the location manager every time a location update error occurs,
 * such as not being able to get a GPS fix.
 */
- (void)locationManager:(CLLocationManager *)manager
        didFailWithError:(NSError *)error
{
    if (delegate != nil)
        [self.delegate locationError:error];
    else
        NSLog(@"%@", @"Failed to report error -- missing delegate.");
}

/**
 * Easy access to the devices current location
 */
- (CLLocation *)getCurrentLocation {
    return [locationManager location];
}

/**
 * Clean up the mess we made
 */
- (void)dealloc {
    [self.locationManager release];
    [super dealloc];
}

@end

To use this, create a new iPhone app, add a WLLocationDriver to your classes interface, and stick these methods in your app delegate.

- (void)viewDidLoad {
    [super viewDidLoad];

    locationDriver = [[WLLocationDriver alloc] init];
    locationDriver.delegate = self;
}

- (void)locationUpdate:(CLLocation *)location {
    NSLog(@"%@", [location description]);
}

- (void)locationError:(NSError *)error {
    NSLog(@"%@", [error description]);
}

Enjoy!

Advertisements