26

I am trying to geocode address into coordinates using following code:

CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    [geocoder geocodeAddressString:@"6138 Bollinger Road, San Jose, United States" completionHandler:^(NSArray* placemarks, NSError* error){
                     for (CLPlacemark* aPlacemark in placemarks)
                     {
                         // Process the placemark.
                         NSString *latDest1 = [NSString stringWithFormat:@"%.4f",aPlacemark.location.coordinate.latitude];
                         NSString *lngDest1 = [NSString stringWithFormat:@"%.4f",aPlacemark.location.coordinate.longitude];
                         lblDestinationLat.text = latDest1;
                         lblDestinationLng.text = lngDest1;
                     }
                 }];

I have tried it many times but the debugger never enters the block and I am not able to get the location. What can I try next?

halfer
  • 19,824
  • 17
  • 99
  • 186
pankaj
  • 7,878
  • 16
  • 69
  • 115
  • Thanks so much, after spending half day i got to the solution from your question. So one UP and Star mark for your question. Thanks again. – Ankit Jain Aug 30 '13 at 11:34
  • how to save the longitude and latitude in a variable outside this block plz ?? Because I need to pass them to another controller. – user3722523 Feb 05 '15 at 21:29
  • @user3722523 you will have to define a variable outside the block and mark it as __block. Now you will be able to access the variable inside the block and you can save values to this variable. – pankaj Feb 07 '15 at 04:24
  • @pankaj I'm using swift and __block doesn't exist anymore. – user3722523 Feb 07 '15 at 22:37

3 Answers3

10

All right I found my mistake. The code is correct and works perfect. All the while I was working on it was through debugger and was trying to figure out why the debugger did not enter the block. But now I have found out debugger does not enter the block at that moment. It takes little in getting the location values. It is done asynchronously so I was not able to find it and I was getting crash because of no values just after the crash. I have moved my code post block to inside the block and everything works fine for me now.

pankaj
  • 7,878
  • 16
  • 69
  • 115
0

I just ran that exact code and it worked as expected. Make sure that you have an active internet connection.

Try adding a NSLog on the strings and see if it gets called.

NSLog(@"lat: %@, lng: %@", latDest1, lngDest1);

Are you running it in the simulator or the device?

Bassem
  • 119
  • 2
  • 7
0

Blocks are new features to Objective C from iOS4.0 onwards. A block you can imagine as a delegate method working in same functional block. As for any delegate method it takes time to invoke, depending upon the condition, same way block executes the code inside it, when it completes its work of geocoding. You can read more about Block in apples documentation or read http://www.raywenderlich.com/9438/how-to-use-blocks-in-ios-5-tutorial-part-2.

You can also have look into my repository on GITHUB https://github.com/Mangesh20/geocoding

mangesh
  • 574
  • 4
  • 16