geocoding address into coordinates in iphone

pankaj picture pankaj · May 24, 2012 · Viewed 17.3k times · Source

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. Can someone please help me with it?

Regards
Pankaj

Answer

pankaj picture pankaj · May 24, 2012

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.