Search code examples
iosiphoneobjective-ccllocationmanagercllocation

CLLocation Manager startUpdatingLocation not working second time called


Hi I am implementing Location services in my app. First I have to know my Coordinates to get the distance between some places that I have in a list and the device. Then if I go into a place I can make a check in, so, I need to get coordinates again, and the problem is here. Second time I try to get coordinates, the method -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations is not called.. and I can not get new Coordinates.

My manager is located in a NSObject sublcass with this code:

(id)init {
    if ( self = [super init] ) {
        if ([CLLocationManager locationServicesEnabled])
        {
            locationManager = [[CLLocationManager alloc] init];
            locationManager.delegate = self;
             [locationManager startUpdatingLocation];
        }
    }
    return self;
}

-(void) checkLongLatitudeAgain {
    [locationManager startUpdatingLocation];
}

#pragma mark Delegates de CLLocationManager
//
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
     NSLog(@"LON%f",  manager.location.coordinate.longitude);
    NSLog(@"LAT:%f", manager.location.coordinate.latitude);
    NSTimeInterval howRecentNewLocation = [newLocationeventDate timeIntervalSinceNow];

    if (manager.location.horizontalAccuracy <= 100.0 && howRecentNewLocation < -0.0 && howRecentNewLocation > -20.0){
        //Usar coordenada
        [self.delegate getLocationForCheckIn:manager.location];
        [self stopUpdatingLocation:@"Fins"];
    }
}
// ---------------------------------------------------------------------------------------------------------------------
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
    //
    if ([error code] != kCLErrorLocationUnknown) {
        [self stopUpdatingLocation:NSLocalizedString(@"Error", @"Error")];
    }
    //
}
// ---------------------------------------------------------------------------------------------------------------------
- (void)stopUpdatingLocation:(NSString *)state {
    //Detenemos la lectura del GPS
    [locationManager stopUpdatingLocation];
    locationManager.delegate = nil;
    NSLog(@"Stop gps");
    //
}

I call the class when the list of places is open, and also when inside a place the user press checkIn button. Both times I do it with this code:

WPLocationManager *location = [[WPLocationManager alloc]init];
[location checkLongLatitudeAgain];

Solution

  • You are creating a new manager every time:

    WPLocationManager *location = [[WPLocationManager alloc]init];
    [location checkLongLatitudeAgain];
    

    That new manager is not assigned to any delegate.

    You need to use the previous manager you have created and assigned to your delegate, something like:

    [locationManager checkLongLatitudeAgain];