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:
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
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;
}];
}
}