Mann
Mann

Reputation: 5507

locationupdate method calls unneccessorly iPhone

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

Upvotes: 1

Views: 240

Answers (2)

Beltalowda
Beltalowda

Reputation: 4678

You might try sending the message startMonitoringSignificantLocationChanges to your CLLocationManager instead of startUpdatingLocation?

Upvotes: 0

AliSoftware
AliSoftware

Reputation: 32681

That's normal it is called twice if it gets a different precision each time.

Check the horizontalAccuracy property of newLocation for each one of the call, The second one will probably be more accurate that the first one that is sent on first approximation (even if you use kCLLocationAccuracyThreeKilometers for your desired accuracy)

That's quite the same as with the "Maps" application that displays your location on the GoogleMaps map: you first have a large blue circle with a very approximate position, then a more accurate position quickly after.

Upvotes: 2

Related Questions