Mason
Mason

Reputation: 7103

iOS forward geocoding block not being executed

Why this block of code isn't being executed? I copied and pasted it from another project of mine, where it works just fine. I also tried it in my other app with the same addressString I'm plugging in here, and it worked perfectly.

    NSString *addressString = [NSString stringWithFormat:@"%@ and %@, %@, NY", street, rightBound, [boroughs objectForKey:borough]];
    NSLog(@"Address string: %@",addressString);

    [geocoder geocodeAddressString:addressString completionHandler:^(NSArray *placemarks, NSError *error)
    {
        NSLog(@"Placemark count:%d",[placemarks count]);

        for(CLPlacemark *placemark in placemarks)
        {
            NSLog(@"%@",placemark);
        }

        if(anError)
        {
            NSLog(@"Error: %@",[error description]);
        }
    }];

Neither any placemarks nor an error message is logged to the console.

Here is my entire AppDelegate.m:

@implementation AppDelegate

@synthesize window = _window;

- (void)dealloc
{
    [_window release];
    [super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    NSError *error = nil;
    SBJsonParser *parser = [[SBJsonParser alloc] init];

    NSString *JSONString = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Streets" ofType:@"json"] encoding:NSUTF8StringEncoding error:&error];

    if(error)
    {
        NSLog(@"%@",[error description]);
        NSLog(@"Break");
    }

    NSDictionary *dict = [parser objectWithString:JSONString error:&error];
    if(error)
    {
        NSLog(@"%@",[error description]);
        NSLog(@"Break");
    }

    NSArray *addresses = [[dict objectForKey:@"results"] retain];

    NSDictionary *boroughs = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"Bronx",@"Brooklyn",@"New York", @"Queens",@"Staten Island",nil] forKeys:[NSArray arrayWithObjects:@"B",@"K",@"M",@"Q",@"S", nil]];

    int i = 1;
    for(NSDictionary *file in addresses)
    {
        NSString *borough = [file objectForKey:@"Borough"];
        NSString *ID = [file objectForKey:@"ID"];
        NSString *leftBound = [file objectForKey:@"LeftBound"];
        NSString *rightBound = [file objectForKey:@"RightBound"];
        NSString *sideOfStreet = [file objectForKey:@"SideOfStreet"];
        NSString *street = [file objectForKey:@"Street"];

        NSString *addressString = [NSString stringWithFormat:@"%@ and %@, %@, NY", street, rightBound, [boroughs objectForKey:borough]];
//        NSLog(@"Address string: %@",addressString);

        CLGeocoder *geocoder = [[CLGeocoder alloc] init];

        [geocoder geocodeAddressString:addressString completionHandler:^(NSArray *placemarks, NSError *anError)
        {
            NSLog(@"AAAAAAAAAAAAAAAAAAAAAAAA");
            NSLog(@"Placemark count:%d",[placemarks count]);

            for(CLPlacemark *placemark in placemarks)
            {
                NSLog(@"Placemark: %@",placemark);
            }

            if(anError)
            {
                NSLog(@"Error: %@",[error description]);
            }
        }];

        [geocoder release];
        NSLog(@"%d",i++);
    }
    [parser release];
    [addresses release];

    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];

    return YES;
}

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
        if([keyPath isEqualToString:@"geocoder"])
        {
            NSLog(@"AAAAAAA");
        }
}

@end

Upvotes: 4

Views: 3209

Answers (3)

Bushbert
Bushbert

Reputation: 158

I know this is an old question but the reason this doesn't work is you cannot make multiple forward requests simultaneously. You must check the property isGeocoding before sending another one.

Upvotes: 0

Luke Fletcher
Luke Fletcher

Reputation: 348

I'm still not sure what's going on. I ran the following code (original code minus the JSON operations) on my simulator and it worked perfectly. Maybe you need a fresh install of Xcode & iOS Simulator?

@implementation AppDelegate

@synthesize window = _window;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    NSString *addressString = [NSString stringWithFormat:@"1 Infinite Loop, Cupertino, CA"];

    CLGeocoder *geocoder = [[CLGeocoder alloc] init];

    [geocoder geocodeAddressString:addressString completionHandler:^(NSArray *placemarks, NSError *anError)
     {
         NSLog(@"AAAAAAAAAAAAAAAAAAAAAAAA");
         NSLog(@"Placemark count:%d",[placemarks count]);

         for(CLPlacemark *placemark in placemarks)
         {
             NSLog(@"Placemark: %@",placemark);
         }

         if(anError)
         {
             NSLog(@"Error: %@",[anError description]);
         }
     }];

    [geocoder release];

    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];

    return YES;
}

@end

Output was:

2011-12-21 18:56:30.311 Test[44445:f803] AAAAAAAAAAAAAAAAAAAAAAAA
2011-12-21 18:56:30.312 Test[44445:f803] Placemark count:1
2011-12-21 18:56:30.314 Test[44445:f803] Placemark: 1 Infinite Loop, Cupertino, CA  95014-2083, United States @ <+37.33168400,-122.03075800> +/- 100.00m, region (identifier <+37.33168400,-122.03075800> radius 71.01) <+37.33168400,-122.03075800> radius 71.01m

The only thing I can think of is that you may be releasing your geocoder too early! Maybe try moving the release into the block? This way, you'll know the geocoder is only being released once after it has finished the geocode operation.

Also, you made a mistake with your error handling inside the block.
It should be NSLog(@"Error: %@",[anError description]);
instead of NSLog(@"Error: %@",[error description]);.

Also, make sure you're not using ARC...

Upvotes: 0

Luke Fletcher
Luke Fletcher

Reputation: 348

Have you made sure your "geocoder" instance is not nil?
Nothing will happen if you send a message to a "nil" object... :)

NSLog(@"%@",geocoder);

Upvotes: 1

Related Questions