Reputation: 359
I am trying to reverse geocode a location, I have managed todo this but I have hit a slight snag which is preventing me from completing it. I think I know WHAT it's doing and WHY, but I am not able to figure out a way to get around it yet. (I am using ARC and iOS5)
The problem occurs when I execute the following code in my init method ...
[self setSubtitle: [dateString stringByAppendingString: self.city ] ];
self.city is returning back a null value, I am pretty sure this is because the init method completes WAY before the geocoder can go and do it's thing.
So my question is, how can I handle this situation? I assume sitting and waiting for the geocoder to complete its work and freezing the app until such time isn't the way to go?
#import "MapPoint.h"
@implementation MapPoint
@synthesize coordinate, title, dateAdded, subtitle, city;
-(id)initWithCoordinates:(CLLocationCoordinate2D)c
title:(NSString *)t {
self = [super init];
if (self) {
coordinate = c;
[self setTitle: t];
[self setCurrentCity: [[CLLocation alloc] initWithLatitude:c.latitude longitude:c.longitude]];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setTimeStyle:NSDateFormatterNoStyle];
[formatter setDateStyle:NSDateFormatterLongStyle];
[self setDateAdded: [[NSDate alloc] init]];
NSString *dateString = [formatter stringFromDate: [self dateAdded]];
[self setSubtitle: [dateString stringByAppendingString: self.city ] ];
}
return self;
}
-(void)setCurrentCity: (CLLocation *)loc {
CLGeocoder *reverseGeo;
if (!reverseGeo) {
reverseGeo = [[CLGeocoder alloc] init];
}
[reverseGeo reverseGeocodeLocation: loc completionHandler:
^(NSArray *placemarks, NSError *error) {
if ([placemarks count] > 0) {
CLPlacemark *topResult = [placemarks objectAtIndex:0];
self.city = [NSString stringWithFormat:@"%@", [topResult locality]];
}
}];
}
@end
Upvotes: 2
Views: 3391
Reputation: 359
turns out I needed to use a for loop to iterate through the results and set the annotation subtitle from here.
#import "MapPoint.h"
@implementation MapPoint
@synthesize coordinate, title, dateAdded, subtitle, city, reverseGeo;
-(id)initWithCoordinates:(CLLocationCoordinate2D)c
title:(NSString *)t {
self = [super init];
if (self) {
coordinate = c;
[self setTitle: t];
[self setCurrentCity: [[CLLocation alloc] initWithLatitude:c.latitude longitude:c.longitude]];
[self setDateAdded: [[NSDate alloc] init]];
}
return self;
}
-(void)setCurrentCity: (CLLocation *)loc {
if (!self.reverseGeo) {
self.reverseGeo = [[CLGeocoder alloc] init];
}
[self.reverseGeo reverseGeocodeLocation: loc completionHandler:
^(NSArray *placemarks, NSError *error) {
for (CLPlacemark *placemark in placemarks) {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setTimeStyle:NSDateFormatterNoStyle];
[formatter setDateStyle:NSDateFormatterLongStyle];
NSString *dateString = [formatter stringFromDate: [self dateAdded]];
[self setSubtitle: [dateString stringByAppendingString: [placemark locality] ] ];
}
}];
}
@end
Upvotes: 2
Reputation: 46965
you need to do something like this in the reverseGeocoder didFindPlacemark method:
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark {
//progressLabel.text = NSLocalizedString(@"Location Determined", @"Location Determined");
MapLocation *annotation = [[[MapLocation alloc] init] autorelease];
annotation.streetNumber = placemark.subThoroughfare;
annotation.streetAddress = placemark.thoroughfare;
annotation.city = placemark.locality;
}
Upvotes: -1