Issue trying to get location address


#1

Hello

this is my consult

I’m building a function which I send inside a string latitude and longitude and gets back address, this is the code I built


-(NSString *)obtieneDireccionCita:(NSString *)coordenadas{
    CLGeocoder *geocoder = [[CLGeocoder alloc]init];
    
    __block NSString *direccionCita = nil;
    [geocoder geocodeAddressString:coordenadas completionHandler:^(NSArray *placemarks, NSError *error) {
        if(error){
            NSLog(@"Failure: %@", [error localizedDescription]);
            return;
        }
        
        CLPlacemark *anotacion = [placemarks objectAtIndex:[placemarks count]-1];
        
        direccionCita = [NSString stringWithFormat:@"%@, %@, %@, %@, %@, %@",
                            anotacion.thoroughfare,
                            anotacion.subLocality,
                            anotacion.postalCode,
                            anotacion.locality,
                            anotacion.administrativeArea,
                            anotacion.ISOcountryCode];
        
       NSLog(@"cita adentro %@", direccionCita); 
    }];
    NSLog(@"cita afuera %@", direccionCita);
    return direccionCita;
}

I added a call on viewdidload method and I got this:


- (void)viewDidLoad
{
    [super viewDidLoad];
    
    locationAppointmentLabel.text = [self obtieneDireccionCita:[NSString stringWithFormat:@"%@,%@",
                                                                self.latitud, self.longitud]];
}

when I run this, this prints next

2012-12-03 18:20:58.303 eCalendar[6186:18203] cita afuera (null)
2012-12-03 18:20:58.828 eCalendar[6186:18203] cita adentro Loma Pinal 53, Los Altos, 22342, Guadalajara, Jalisco, MX

the thing is this function first returns direccionCita empty and then fills it with location address result and I want this to return direccionCita until it gets result

thanks in advance


#2

I’m gonna share my solution with everyone:

-(NSMutableArray *)obtieneDireccionCita:(NSString *)coordenadas{
    CLGeocoder *geocoder = [[CLGeocoder alloc]init];
    
    __block NSMutableArray *direccionCita = [[NSMutableArray alloc]init];
    
    while ([direccionCita count] == 0) {
        [geocoder geocodeAddressString:coordenadas completionHandler:^(NSArray *datosDireccion, NSError *error) {
            if(error){
                NSLog(@"Failure: %@", [error localizedDescription]);
                [direccionCita addObject:@"Dirección no encontrada"];
                return;
            }
            
            CLPlacemark *direccion = [datosDireccion objectAtIndex:[datosDireccion count]-1];
            
            [direccionCita addObject:direccion.thoroughfare];
            [direccionCita addObject:direccion.subLocality];
            [direccionCita addObject:direccion.postalCode];
            [direccionCita addObject:direccion.locality];
            [direccionCita addObject:direccion.administrativeArea];
            [direccionCita addObject:direccion.ISOcountryCode];

        }];
        [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
    }
    return direccionCita;
}

changed this function to a separate NsObject called functions
and in my didloadEvent i added this

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //Loads the object with generic functions
    funciones = [[Functions alloc]init];


    //Si latitud y longitud tienen valores validos ejecutan esta función
    //retorna un array con todos los datos de la dirección
    NSString *completeAddress = Nil;
    if (![latitud isEqualToString:Nil] && ![longitud isEqualToString:Nil]){
        NSMutableArray *addressArray = [NSMutableArray arrayWithArray:[funciones obtieneDireccionCita:
                                                                       [NSString stringWithFormat:@"%@,%@",
                                                                        self.latitud,self.longitud]]];
        
        for (NSInteger i = 0; i < [addressArray count]; i++) {
            
            if (completeAddress == Nil)
                completeAddress = [addressArray objectAtIndex:i];
            else
            completeAddress = [NSString stringWithFormat:@"%@, %@", completeAddress, [addressArray objectAtIndex:i]];
        }

        self.addressAppointmentTextView.text = completeAddress;
    }
}

I just added my answer inside a NSMutableArray to make this more generic and if I wanna get just certain info of the address for filling separate fields comming from webservices… who knows

thanks in advance

Juan