Skip to content

Instantly share code, notes, and snippets.

@kwylez
Created April 19, 2012 15:51
Show Gist options
  • Save kwylez/2421914 to your computer and use it in GitHub Desktop.
Save kwylez/2421914 to your computer and use it in GitHub Desktop.
Shared core location manager
#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
@interface CWCLocationManager : NSObject <CLLocationManagerDelegate> {
CLLocationManager *locationManager;
CLLocation *currentLocation;
NSDate *locationManagerStartDate;
NSTimer *locationTimer;
}
@property (nonatomic, retain) CLLocation *currentLocation;
@property (nonatomic, retain) NSDate *locationManagerStartDate;
@property (nonatomic, retain) NSTimer *locationTimer;
+ (CWCLocationManager *)sharedInstance;
- (void)start;
- (void)stop;
@end
#import "CWCLocationManager.h"
@interface CWCLocationManager()
- (BOOL)isValidLocation:(CLLocation *)newLocation withOldLocation:(CLLocation *)oldLocation;
@end
@implementation CWCLocationManager
@synthesize currentLocation;
@synthesize locationManagerStartDate;
@synthesize locationTimer;
static CWCLocationManager *sharedManager;
+ (CWCLocationManager *)sharedInstance {
static dispatch_once_t pred;
dispatch_once(&pred, ^{
sharedManager = [[CWCLocationManager alloc] init];
});
return sharedInstance;
}
- (void)dealloc {
[currentLocation release];
locationManager.delegate = nil;
[locationManager release];
[super dealloc];
}
- (id)init {
self = [super init];
if (self) {
currentLocation = [[CLLocation alloc] init];
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
locationManager.distanceFilter = 100;
[self start];
locationManagerStartDate = [[NSDate date] retain];
}
return self;
}
#pragma mark - Public Methods
- (void)start {
[locationManager startUpdatingLocation];
self.locationTimer = [NSTimer scheduledTimerWithTimeInterval:30.0
target:self
selector:@selector(stop)
userInfo:nil
repeats:NO];
}
- (void)stop {
[locationManager stopUpdatingLocation];
[self.locationTimer invalidate];
}
#pragma mark - Private Methods
/**
* Reference: https://gist.github.com/1653505
*/
- (BOOL)isValidLocation:(CLLocation *)newLocation withOldLocation:(CLLocation *)oldLocation {
// Filter out nil locations
if (!newLocation) {
return NO;
}
// Filter out points by invalid accuracy
if (newLocation.horizontalAccuracy < 0) {
return NO;
}
// Filter out points that are out of order
NSTimeInterval secondsSinceLastPoint = [newLocation.timestamp timeIntervalSinceDate:oldLocation.timestamp];
if (secondsSinceLastPoint < 0) {
return NO;
}
// Filter out points created before the manager was initialized
NSTimeInterval secondsSinceManagerStarted = [newLocation.timestamp timeIntervalSinceDate:locationManagerStartDate];
if (secondsSinceManagerStarted < 0) {
return NO;
}
// The newLocation is good to use
return YES;
}
#pragma mark - Delegate Methods
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
NSInteger locationAge = abs([newLocation.timestamp timeIntervalSinceDate:[NSDate date]]);
/**
* 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 (locationAge > 120 || ![self isValidLocation:newLocation withOldLocation:oldLocation]) {
return;
}
self.currentLocation = newLocation;
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Error", nil)
message:[error description]
delegate:nil
cancelButtonTitle:NSLocalizedString(@"OK", nil)
otherButtonTitles:nil];
[alert show];
[alert release];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment