CLGeocoder and backward compatibility
Asked Answered
F

3

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!

Ferri answered 18/10, 2011 at 14:43 Comment(0)
D
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

Dally answered 18/10, 2011 at 15:18 Comment(4)
I got message like : Error Domain=NSURLErrorDomain Code=-1011 "The operation couldn’t be completed. (NSURLErrorDomain error -1011.)". And it worked before...Protease
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)Dally
Ok. Now it works. That was just temporary and that was a very bad coincidence with my iOS5 upgrade... Thank you for your help!Protease
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 OSDally
G
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;

}];
Garcon answered 5/12, 2011 at 10:33 Comment(2)
How to initialize CLGeoCoder object?Matney
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 - maps.googleapis.com/maps/api/geocode/…, it does returns full Postal code!Buskirk
D
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

Dally answered 18/10, 2011 at 15:18 Comment(4)
I got message like : Error Domain=NSURLErrorDomain Code=-1011 "The operation couldn’t be completed. (NSURLErrorDomain error -1011.)". And it worked before...Protease
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)Dally
Ok. Now it works. That was just temporary and that was a very bad coincidence with my iOS5 upgrade... Thank you for your help!Protease
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 OSDally
S
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

Stopwatch answered 17/7, 2012 at 9:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.