It's not really a definitive, perfect algorithm (I doubt there is one for this task, because of external conditions, I mean, you can try get ur location on plain field or inside a tomb), it's an ad-hoc one, and it works for me.
I did a wrapper for LocationManager, like
@protocol LocationManagerWrapperDelegate <NSObject>
@required
- (void) locationUpdated: (CLLocation *) locationUpdate;
- (void) errorOccured: (NSError *) error;
@end
@interface LocationManagerWrapper : NSObject <CLLocationManagerDelegate>
{
CLLocationManager *locationManager;
id delegate;
CLLocation *mostAccurateLocation;
int updatesCounter;
BOOL m_acceptableTimePeriodElapsed;
}
@property (nonatomic, retain) CLLocationManager *locationManager;
@property (nonatomic, retain) CLLocation *mostAccurateLocation;
@property (nonatomic, assign) id <LocationManagerWrapperDelegate> delegate;
- (void) startUpdatingLocation;
- (void) locationManager: (CLLocationManager *) manager
didUpdateToLocation: (CLLocation *) newLocation
fromLocation: (CLLocation *) oldLocation;
- (void) locationManager: (CLLocationManager *) manager
didFailWithError: (NSError *) error;
+ (LocationManagerWrapper *) sharedInstance;
@end
Implementation
#define NUMBER_OF_TRIES 4
#define ACCEPTABLE_TIME_PERIOD 15.0
- (void) startUpdatingLocation
{
NSAssert(self.delegate != nil, @"No delegate set to receive location update.");
updatesCounter = 0;
self.mostAccurateLocation = nil;
m_acceptableTimePeriodElapsed = NO;
[NSTimer scheduledTimerWithTimeInterval:ACCEPTABLE_TIME_PERIOD
target:self
selector:@selector(acceptableTimePeriodElapsed:)
userInfo:nil
repeats:NO];
[self.locationManager startUpdatingLocation];
}
- (void) acceptableTimePeriodElapsed: (NSTimer *) timer
{
@synchronized(self)
{
m_acceptableTimePeriodElapsed = YES;
// TODO: if period is set by user - check we have mostAccurateLocation at this point
[self.delegate locationUpdated:self.mostAccurateLocation];
[self.locationManager stopUpdatingLocation];
}
}
- (void) locationManager: (CLLocationManager *) manager
didUpdateToLocation: (CLLocation *) newLocation
fromLocation: (CLLocation *) oldLocation
{
@synchronized(self)
{
if (m_acceptableTimePeriodElapsed) return;
NSLog([NSString stringWithFormat:@"lat: %@, long: %@, acc: %@",
[ [NSNumber numberWithDouble:newLocation.coordinate.latitude] stringValue],
[ [NSNumber numberWithDouble:newLocation.coordinate.longitude] stringValue],
[ [NSNumber numberWithDouble:newLocation.horizontalAccuracy] stringValue] ] );
updatesCounter++;
// ignore first returned value
if (updatesCounter <= 1) return;
if (self.mostAccurateLocation == nil ||
self.mostAccurateLocation.horizontalAccuracy > newLocation.horizontalAccuracy)
{
self.mostAccurateLocation = newLocation;
}
if (updatesCounter >= NUMBER_OF_TRIES)
{
[self.delegate locationUpdated:self.mostAccurateLocation];
[self.locationManager stopUpdatingLocation];
}
}
}
The code is not excellent (neither formatting is), but the idea, I think, is simple and clear, get first location, throw it out, it's a cached one, do 3 tries max for a the most accurate location. It can take a long time, and if user is waiting (as in my case), define a time limit. One more time, it works for my app, but feel free to tweak it or take another approach.