CLGeocoder
happens asynchronously (the fact that it's done in a block gives that away), and thus _latitudes
and _longitudes
are not set by the time the method, itself, returns. So, likely, yes, some slight restructuring of your code will be needed. It's hard for us to advise as we don't see the code that calls this routine. Bottom line, you have to refactor that code so that rather than returning the location
, you have the completionHandler
of geocodeAddressString
invoke whatever you need done.
Looking at your revised code, it appears that your viewDidLoad
is taking the location coordinates and creating an annotation. Just do that adding of the annotation inside the geocoded block.
Thus:
-(CLLocationCoordinate2D) geocodeAddressString:(NSString*)addressStr title:(NSString *)title subtitle:(NSString *)subtitle
{
CLGeocoder *geocoder = [[[CLGeocoder alloc] init] autorelease];
[geocoder geocodeAddressString:addressStr completionHandler:^(NSArray *placemarks, NSError *error) {
self.placemarksArray = placemarks;
CLPlacemark *placeInfo = [placemarks objectAtIndex:0];
id<MKAnnotation> annotation = [[[MyAddressAnnotation alloc] initWithCoordinate:placeInfo.location.coordinate
title:title
SubTitle:subtitle] autorelease];
[self.mapView addAnnotation:addAnnotation];
}];
}
And then, viewDidLoad
would call it as follows:
[self geocodeAddressString:fullAddress title:firstName subtitle:lastName];