Unable to execute first iOS geocoding unit

Why is this block of code not executing? I copied and pasted it from my other project, where it works perfectly. I also tried this in my other application with the same addressString that I am connecting 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]); } }]; 

No tags or error messages are logged in the console.

Here is my whole 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 
+4
source share
3 answers

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

 NSLog(@"%@",geocoder); 
+1
source

I'm still not sure what is going on. I executed the following code (source code minus JSON operations) on my simulator and it worked perfectly. Maybe you need a new installation of Xcode and 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 

The 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 can release your geocoder sooner or later! Maybe try moving the release to block? This way you will find out that the geocoder is freed only after the geocoding operation is completed.

In addition, you made a mistake in handling errors inside the block.
It must be NSLog(@"Error: %@",[anError description]);
instead of NSLog(@"Error: %@",[error description]); .

Also, make sure you are not using ARC ...

0
source

I know this is an old question, but the reason for this does not work, since you cannot simultaneously execute several queries ahead. You should check the isGeocoding property before sending another.

0
source

Source: https://habr.com/ru/post/1386890/


All Articles