I am making my app using coreLocation. Below is my class and it send location updates to delegate class which receives location coordinates and use them.
Problem is when i start my app first time i go to home view(the class which use location coordinates is uiTableView and second one i.e next to home view ) it does not call didUpdateToLocation which is good because the view is not yet loaded.
Then when its view loads it gets called twice it should get called once. then again when i process my app n go to home view and press central button of iPhone, the app goes to background and then again when i start it on home view locationDidUpload method gets called 4 times though view is not yet loaded in which it is implemented. I dont want this because it makes system busy. Please suggest.
- (id)init {
self = [super init];
if(self != nil) {
self.locMgr = [[[CLLocationManager alloc] init] autorelease]; // Create new instance of locMgr
self.locMgr.delegate = self; // Set the delegate as self.
self.locMgr.desiredAccuracy=kCLLocationAccuracyThreeKilometers;//check
self.locMgr.distanceFilter=500;//check in meters
[self.locMgr startUpdatingLocation];
}
return self;
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
if([self.delegate conformsToProtocol:@protocol(CoreLocationControllerDelegate)]) { // Check if the class assigning itself as the delegate conforms to our protocol. If not, the message will go nowhere. Not good.
NSLog(@"core");
[self.delegate locationUpdate:newLocation];
}
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
if([self.delegate conformsToProtocol:@protocol(CoreLocationControllerDelegate)]) { // Check if the class assigning itself as the delegate conforms to our protocol. If not, the message will go nowhere. Not good.
if (error.code==kCLErrorDenied) {
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Permission Denied" message:@"Please allow location to retrieve location" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil , nil];
[alert show];
[alert release];
[locMgr stopUpdatingLocation];
[locMgr release];
locMgr=nil;
}
[self.delegate locationError:error];
}
}
- (void)dealloc {
[self.locMgr release];
[super dealloc];
}
@end
You might try sending the message
startMonitoringSignificantLocationChangesto yourCLLocationManagerinstead ofstartUpdatingLocation?