6

I have a simple question.

MKReverseCoder is deprecated and doesn't work since iOS 5. We have to use CLGeocoder. But, there are a lot of people under iOS4. How the new apps can work with iOS4 and iOS5 for geocoding ?

Thanks!

The iOSDev
  • 5,237
  • 7
  • 41
  • 78
Mathieu Mahé
  • 2,647
  • 3
  • 35
  • 50

3 Answers3

8

If anyone is trying to move onto CLGeocoder from MKReverseGeocoder then I have written a blog post that might be of help http://jonathanfield.me/jons-blog/clgeocoder-example.html

Basically an example would be, after you have created locationManager and CLGeocoder objects just add this code to your viewDidLoad() and then make some labels or text areas to show the data.

[super viewDidLoad]; locationManager.delegate = self; [locationManager startUpdatingLocation];

locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
[self.CLGeocoder reverseGeocodeLocation: locationManager.location completionHandler: 
 ^(NSArray *placemarks, NSError *error) {

     CLPlacemark *placemark = [placemarks objectAtIndex:0];

         isoCountryCode.text = placemark.ISOcountryCode;
         country.text = placemark.country;
         postalCode.text= placemark.postalCode;
         adminArea.text=placemark.administrativeArea;
         subAdminArea.text=placemark.subAdministrativeArea;
         locality.text=placemark.locality;
         subLocality.text=placemark.subLocality;
         thoroughfare.text=placemark.thoroughfare;
         subThoroughfare.text=placemark.subThoroughfare;
         //region.text=placemark.region;

}];
Jon Lin
  • 142,182
  • 29
  • 220
  • 220
Jonathan Field
  • 263
  • 2
  • 8
  • How to initialize CLGeoCoder object? – Satyam Jan 19 '12 at 18:48
  • This doesn't give full postal code! It gives something like "HP2 7", the last two characters are missing. Do you know what could be the reason? If I query Google API - http://maps.googleapis.com/maps/api/geocode/json?latlng=%f,%f&sensor=true, it does returns full Postal code! – Paresh Masani Mar 22 '12 at 15:05
4

MKReverseGeocoder still works with iOS5. It's just deprecated, which means it'll be removed at some later point (like at the release of something like iOS6). So you can continue to use it now without any issues

justin
  • 5,811
  • 3
  • 29
  • 32
  • I got message like : Error Domain=NSURLErrorDomain Code=-1011 "The operation couldn’t be completed. (NSURLErrorDomain error -1011.)". And it worked before... – Mathieu Mahé Oct 18 '11 at 15:45
  • That's definitely odd. I believe that's an authentication error, but finding the source of it will be a problem. It seems like MKReverseGeocoder still works on my app (built for iOS4.3, but running on an iOS5 device), so I feel like the error is coming from elsewhere (or could be a temporary issue with the google servers) – justin Oct 18 '11 at 16:29
  • Ok. Now it works. That was just temporary and that was a very bad coincidence with my iOS5 upgrade... Thank you for your help! – Mathieu Mahé Oct 18 '11 at 16:36
  • My pleasure, and I'm glad to hear it's working now. But yeah, don't worry too much about the depricated methods. They will work for a good while before they are actually removed, giving you and your users plenty of time to switch to the new methods and new OS – justin Oct 18 '11 at 17:25
1
- (IBAction)geoCodeLocation:(id)sender{
    [self.geoCoder reverseGeocodeLocation: locationManager.location completionHandler: 
     ^(NSArray *placemarks, NSError *error) {
         CLPlacemark *placemark = [placemarks objectAtIndex:0];
         NSLog(@"placemark %@",placemark);
         //String to hold address
         NSString *locatedAt = [[placemark.addressDictionary valueForKey:@"FormattedAddressLines"] componentsJoinedByString:@", "];
         NSLog(@"addressDictionary %@", placemark.addressDictionary);

         NSLog(@"placemark %@",placemark.region);
         NSLog(@"placemark %@",placemark.country);  // Give Country Name 
         NSLog(@"placemark %@",placemark.locality); // Extract the city name 
         NSLog(@"location %@",placemark.name);
         NSLog(@"location %@",placemark.ocean);
         NSLog(@"location %@",placemark.postalCode);
         NSLog(@"location %@",placemark.subLocality);

         NSLog(@"location %@",placemark.location);
          //Print the location to console
         NSLog(@"I am currently at %@",locatedAt);

         //Set the label text to current location
         [locationLabel setText:locatedAt];

     }];

For more see property of CLPlacemark

The iOSDev
  • 5,237
  • 7
  • 41
  • 78
krishnendra
  • 673
  • 7
  • 8