First off, I am very new to iOS Dev and Objective-C. So please excuse any stupid questions or code.
I have been testing the location services on an iPhone. I have this code that is fired off by a NSTimer:
- (void)startLocationTracking
{
if(self.locationManager==nil){
_locationManager=[[CLLocationManager alloc] init];
_locationManager.delegate=self;
_locationManager.desiredAccuracy=kCLLocationAccuracyBest;
_locationManager.distanceFilter=1;
self.locationManager=_locationManager;
}
if([CLLocationManager locationServicesEnabled]){
[self.locationManager startUpdatingLocation];
}
}
And here is my location manager function:
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
[self timerLog];
NSString *deviceID = [self getUUID];
double lat = newLocation.coordinate.latitude;
double lon = newLocation.coordinate.longitude;
double alt = newLocation.altitude;
double dir = newLocation.course;
double spd = newLocation.speed;
double ha = newLocation.horizontalAccuracy;
double va = newLocation.verticalAccuracy;
NSDateFormatter *formatter;
NSString *ts;
formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSS"];
ts = [formatter stringFromDate:[NSDate date]];
[self geoTrackingWS :deviceID :lat :lon :alt :dir :spd :ha :va :ts];
[manager stopUpdatingLocation];
}
For some reason my function geoTrackingWS is firing off multiple times randomly. The NSTimer is running every 1 minute (just as a test) and sometimes it works fine and it calls the geoTrackingWS just once but other times it hits 2 or 3 times.
I have done logging and I can see my NSTimer is working fine and firing off as it should.
I have a feeling it has something to do with another application on my phone but I am not sure.
Any help or insight on this would be great.
Thanks
The location manager usually has multiple ways of determining your location, including:
The first of those is least accurate but easiest to get, the second is usually more accurate but takes a while longer to figure out (as there’s a network request for the lookup) and the final is most accurate but can take quite a while to figure out (searching for satellites, etc).
As a result it is expected behaviour that the location manager will respond with increasingly accurate results the longer it’s left running. You can see the consequence of that when running Maps for example — it usually pins you down to quite a wide area quite quickly and then takes some time to get a more accurate estimate.