Skip to content

Instantly share code, notes, and snippets.

@corbett
Created March 17, 2012 15:55
Show Gist options
  • Save corbett/2061433 to your computer and use it in GitHub Desktop.
Save corbett/2061433 to your computer and use it in GitHub Desktop.
Objective-C to obtain quick and accurate location in iOS
// setup
- (void)viewDidLoad
{
...
self.locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
locationManager.distanceFilter = 250;
self.desiredLocationFreshness = 15.0; // desired freshness in s
self.desiredLocationAccuracy = 100.0; // desired location accuracy in m
self.improvementAccuracyToGiveUpOn = 30.0; // desired improvement in m
self.timeToFindLocation = 30.0; // timeout to find location in s
self.locationManager.delegate = self;
/*
* Start in code with
* self.locationTimer = [NSTimer scheduledTimerWithTimeInterval:self.timeToFindLocation
* target:self
* selector:@selector(stopUpdatingLocations) userInfo:nil repeats:NO];
* [locationManager startUpdatingLocation];
* stopped via [locationManager stopUpdatingLocation]; or timeout
*
*/
...
}
// called when location is updated
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
NSDate* eventDate = newLocation.timestamp;
NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
if (abs(howRecent) > self.desiredLocationFreshness || newLocation.horizontalAccuracy < 0) {
// This location is way too old or straight up inaccurate. Keep trying to get better.
if(self.userLocation==nil) {
// Storing this anyway as the best location we have so far, unfortunately.
self.locationRecent=NO;
self.locationAccurate=NO;
self.userLocation=newLocation;
}
}
else if(newLocation.horizontalAccuracy > self.desiredLocationAccuracy && (oldLocation.horizontalAccuracy==0.0||( oldLocation.horizontalAccuracy-newLocation.horizontalAccuracy) > self.improvementAccuracyToGiveUpOn)) {
// Still too inaccurate but we are improving by enough over time or it's our first try. Keep trying to get better.
if((self.userLocation ==nil) || (newLocation.horizontalAccuracy < self.userLocation.horizontalAccuracy)) {
// Storing this anyway as the best location we have so far, unfortunately.
self.locationRecent=YES;
self.locationAccurate = NO;
self.userLocation=newLocation;
}
}
else {
// OK everyone-we either have a great location or the location isn't total crap but we're not improving by enough over time. Time to call it quits.
[self.locationManager stopUpdatingLocation];
self.locationRecent=YES;
self.locationAccurate = YES;
self.userLocation=newLocation;
[self finalizeLocationSearch];
}
}
@amanmeghrajani
Copy link

Hi Corbett!

I am building a location based app in Swift 3. I am doing the things that you describe ablove, but for some reason it takes time to get the initial location fix on my phone, is there a way we can do this better? (like uber getting location instantly). Thanks in advance

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment