I am currently trying to write an iOS app that logs the states that a user has visited.
The issue I am facing is that my (void) locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations method is being called over and over again. I don't necessarily see this as an issue except in that method, I am calling another method that reverse geocodes the CLLocation object and gives the state name.
The error I am getting is:
Geocode failed with error: Error Domain=kCLErrorDomain Code=2 "The operation couldn’t be completed. (kCLErrorDomain error 2.)"
I understand that I am reaching the limit on reverseGeoCoding over a certain timelimit, I am just not sure how to limit it.
Here is my code:
CLLocation *currentLocation;
- (void) getLocation
{
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error)
{
if (error){
NSLog(@"Geocode failed with error: %@", error);
return;
}
if(placemarks && placemarks.count > 0)
{
CLPlacemark *placemark= [placemarks objectAtIndex:0];
currentState = [NSString stringWithFormat:@"%@",[placemark administrativeArea]];
}
//Checks to see if the state exists in the textFile, if not it writes it to the file
if(![newState checkIfStateExists:currentState])
{
[newState writeToFile:currentState];
}
}];
}
-(void) locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
//just a thought, didn't work
//if(![[locations lastObject] isEqual:currentLocation])
currentLocation = [locations lastObject];
[self getLocation];
}
-(void) initializeGPS
{
self.locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
locationManager.distanceFilter = kCLDistanceFilterNone;
[locationManager startUpdatingLocation];
}
- (void) viewDidLoad
{
[self initializeGPS];
[super viewDidLoad];
}
This code works perfectly in the fact that it gets the location in GPS coordinates, translates it into a state name, and writes that state name to a file.
It is just being called too many times, and I am unsure how to limit the number of times that (void) locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locationsis called.
Use the distanceFilter property of the CLLocationManager as outlined here: https://developer.apple.com/library/mac/#documentation/CoreLocation/Reference/CLLocationManager_Class/CLLocationManager/CLLocationManager.html
the distanceFilter property allows you to set the distance in meters the device must travel before the location is updated.
Hope this helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With