0

I have a problem with CLGeocoder, if I try to gain the address name using CLGeocoder and the user current coordinates it correctly returns the address name, postal code, state, etc. in the NSLog but if I try to send those informations in a NSString [contained in a NSMutableArray, but I don't think this is the problem] it returns a (null) value. I suspect this problem is caused by the block I have used in the code, but I am not sure, and I don't know how to fix this problem anyway.

Here's my code:

- (NSArray *)dataArrayPosizione
{
    if (self.arrayPosizione == nil)
    {

        reverseGeocoder = [[CLGeocoder alloc] init];

        locationToReverseGeocode = [[CLLocation alloc]initWithLatitude:self.mapView.userLocation.location.coordinate.latitude longitude:self.mapView.userLocation.location.coordinate.longitude];

        [reverseGeocoder reverseGeocodeLocation:locationToReverseGeocode completionHandler:^(NSArray *placemarks, NSError *error)
        {
            if (placemarks && placemarks.count > 0)
            {
                placemark = placemarks[0];

                NSLog(@"\n%@ %@\n%@, %@, %@\n%@", [placemark subThoroughfare],[placemark thoroughfare], [placemark locality], [placemark administrativeArea], [placemark postalCode], [placemark country]);
            }
        }];

        informazioniPosizione = [NSMutableArray new];

        [informazioniPosizione addObject:[NSString stringWithFormat:@"Indirizzo: %@", [placemark country]]];
        [informazioniPosizione addObject:[NSString stringWithFormat:@"Latitudine: %g\u00B0\nLongitudine: %g\u00B0", self.mapView.userLocation.location.coordinate.latitude, self.mapView.userLocation.location.coordinate.longitude]];

        self.arrayPosizione = informazioniPosizione;
    }

    return self.arrayPosizione;
}

And a picture: enter image description here

Can someone please help me with this? If the posted code is not enough for you to understand the problem I will paste more lines.

Thanks

Aluminum
  • 2,932
  • 5
  • 35
  • 63

2 Answers2

0

You are creating your string before the geocoding block executes. It should look like this:

- (NSArray *)dataArrayPosizione
{
    if (self.arrayPosizione == nil)
    {

        reverseGeocoder = [[CLGeocoder alloc] init];

        locationToReverseGeocode = [[CLLocation alloc]initWithLatitude:self.mapView.userLocation.location.coordinate.latitude longitude:self.mapView.userLocation.location.coordinate.longitude];

        [reverseGeocoder reverseGeocodeLocation:locationToReverseGeocode completionHandler:^(NSArray *placemarks, NSError *error)
        {
            informazioniPosizione = [NSMutableArray new];

            if (placemarks && placemarks.count > 0)
            {
                placemark = placemarks[0];

                NSLog(@"\n%@ %@\n%@, %@, %@\n%@", [placemark subThoroughfare],[placemark thoroughfare], [placemark locality], [placemark administrativeArea], [placemark postalCode], [placemark country]);

                [informazioniPosizione addObject:[NSString stringWithFormat:@"Indirizzo: %@", [placemark country]]];
                [informazioniPosizione addObject:[NSString stringWithFormat:@"Latitudine: %g\u00B0\nLongitudine: %g\u00B0", self.mapView.userLocation.location.coordinate.latitude, self.mapView.userLocation.location.coordinate.longitude]];

            }

            self.arrayPosizione = informazioniPosizione;

            return self.arrayPosizione;
        }];

    }

}
bachonk
  • 3,924
  • 1
  • 14
  • 14
  • Hello @bachonk, I have tried your code but this is the result :-/ http://i.imgur.com/kCo7ZvQ.png thanks for your answer anyway :-) – Aluminum Jan 27 '14 at 00:47
  • You were returning an empty array before the block was executed. I updated my code accordingly, but note that this is not the most recommended way to handle this. – bachonk Jan 27 '14 at 00:52
  • Nope, what code and where should I put to refresh my `UITableView`? Thank you for your help and your time :-) – Aluminum Jan 27 '14 at 00:57
  • Assuming that your table view is using `self.arrayPosizione` in its data source, you'll want to call `[tableView reloadData]` immediately after setting that value in the line `self.arrayPosizione = informazioniPosizione;` – bachonk Jan 27 '14 at 00:59
0

CLGeocoder's block will get executed asynchronously and therefore the placemark variable is not set yet when you try to use it. Like bachonk suggests, move the code within the geocoder's block. After you add the placemark's info to the array you simply have to call -[UITableView reloadData] on your tableView and the info will appear.

Gianluca Tranchedone
  • 3,598
  • 1
  • 18
  • 33