logicaroma
4/8/2011 - 1:07 PM

An Objective-C Object for LocationServices

An Objective-C Object for LocationServices

// LocationServices.h

#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>


@interface LocationServices : NSObject <CLLocationManagerDelegate> {
	CLLocationManager *locationManager;
	CLLocation *currentLocation;
	
}

@property (nonatomic, retain) CLLocationManager *locationManager;
@property (nonatomic, retain) CLLocation *currentLocation;

- (void)startLocationServices;

@end

// LocationServices.m

#import "LocationServices.h"


@implementation LocationServices

@synthesize locationManager, currentLocation;

- (void)startLocationServices {
	locationManager = [[CLLocationManager alloc] init];
	locationManager.delegate = self;
	
	if ([CLLocationManager locationServicesEnabled]) {
		[locationManager startUpdatingLocation];
	} else {
		NSLog(@"Location services is not enabled");
	}
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
	self.currentLocation = newLocation;
	
	NSLog(@"Latidude %@ Longitude: %@", newLocation.coordinate.latitude, newLocation.coordinate.longitude);
}

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
	[locationManager stopUpdatingLocation];
	NSLog(@"Update failed with error: %@", error);
}

@end