Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #import <CoreLocation/CoreLocation.h>
- #import <Foundation/Foundation.h>
- @interface LocationController : NSObject <CLLocationManagerDelegate> {
- CLLocationManager *locationManager;
- CLLocation *currentLocation;
- }
- + (LocationController *)sharedInstance;
- -(void) start;
- -(void) stop;
- -(BOOL) locationKnown;
- @property (nonatomic, retain) CLLocation *currentLocation;
- @end
- @implementation LocationController
- @synthesize currentLocation;
- static LocationController *sharedInstance;
- + (LocationController *)sharedInstance {
- @synchronized(self) {
- if (!sharedInstance)
- sharedInstance=[[LocationController alloc] init];
- }
- return sharedInstance;
- }
- +(id)alloc {
- @synchronized(self) {
- NSAssert(sharedInstance == nil, @"Attempted to allocate a second instance of a singleton LocationController.");
- sharedInstance = [super alloc];
- }
- return sharedInstance;
- }
- -(id) init {
- if (self = [super init]) {
- self.currentLocation = [[CLLocation alloc] init];
- locationManager = [[CLLocationManager alloc] init];
- locationManager.delegate = self;
- [self start];
- }
- return self;
- }
- -(void) start {
- [locationManager startUpdatingLocation];
- }
- -(void) stop {
- [locationManager stopUpdatingLocation];
- }
- -(BOOL) locationKnown {
- if (round(currentLocation.speed) == -1) return NO; else return YES;
- }
- - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
- //if the time interval returned from core location is more than two minutes we ignore it because it might be from an old session
- if ( abs([newLocation.timestamp timeIntervalSinceDate: [NSDate date]]) < 120) {
- self.currentLocation = newLocation;
- }
- }
- - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
- UIAlertView *alert;
- alert = [[UIAlertView alloc] initWithTitle:@"Error" message:[error description] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
- [alert show];
- [alert release];
- }
- -(void) dealloc {
- [locationManager release];
- [currentLocation release];
- [super dealloc];
- }
- @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement