0

It say it is "deprecated in iOS 5".

- (void)viewDidLoad {
    [super viewDidLoad];
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];

    [geocoder reverseGeocodeLocation:self.locationManager.location // You can pass aLocation here instead
                   completionHandler:^(NSArray *placemarks, NSError *error) {

                 dispatch_async(dispatch_get_main_queue() , ^ {
                           // do stuff with placemarks on the main thread

                           CLPlacemark *place = [placemarks objectAtIndex:0];

                           NSString *zipString = [place.addressDictionary valueForKey:@"ZIP"];

                           [self performSelectorInBackground:@selector(showWeatherFor:) withObject:zipString];



                       }
            }
 }
Alex
  • 8,245
  • 8
  • 46
  • 55
user1555256
  • 115
  • 1
  • 8
  • I'm sorry, I don't quite understand what the question is - can you be a little more clear about what exactly you're asking? – Tim Aug 06 '12 at 16:22
  • When when i type MKReverseGeocoder in xcode it says that it is "deprecated in iOS 5". does that help? – user1555256 Aug 06 '12 at 16:25

2 Answers2

14

MKReverseGeocoder is deprecated in all firmwares after iOS4. This just means that it is now obsolete and frowned upon to be using the outdated class. Use CLGeocoder instead, like so:

    CLGeocoder *geocoder = [[CLGeocoder alloc] init];

        [geocoder reverseGeocodeLocation:self.locationManager.location // You can pass aLocation here instead 
                       completionHandler:^(NSArray *placemarks, NSError *error) {

                           dispatch_async(dispatch_get_main_queue(),^ {
                               // do stuff with placemarks on the main thread

                           if (placemarks.count == 1) {

                           CLPlacemark *place = [placemarks objectAtIndex:0];


                           NSString *zipString = [place.addressDictionary valueForKey:@"ZIP"];

                           [self performSelectorInBackground:@selector(showWeatherFor:) withObject:zipString];

                           }

     });

}];

If you want to reverse geocode a hardcoded pair of coordinates perse --

Initialize a CLLocation location with your latitude and longitude:

    CLLocation *aLocation = [[CLLocation alloc] initWithLatitude:latitude longitude:longitude];

I also want to point out that you can still use MKReverseGeocoder. It may be removed with future iOS updates though.

Imirak
  • 1,323
  • 11
  • 21
  • Thanks for sharing i seem to be getting errors? do i put inside the if statement? – user1555256 Aug 06 '12 at 16:43
  • Well how are you using it? What errors are you getting? It depends on the context. The code above simply reverse geocodes the current location of the user. I've edited my answer so that you can reverse geocode a **specific** location. – Imirak Aug 06 '12 at 17:21
  • Ok because now my app is not showing its current location. Also where would i implant your code into my code? – user1555256 Aug 06 '12 at 18:01
  • It depends. Can you provide some context? And what errors are you getting? – Imirak Aug 06 '12 at 18:02
  • [self performSelectorInBackground:@selector(showWeatherFor:) withObject:@"95301"]; this is all that works. but does not give current location only the zip code entered in the code. i want my app to find your location. the errors are expected ). no @end. – user1555256 Aug 06 '12 at 18:09
  • I don't really get what you are trying to accomplish. Are you just trying to get the user's current location? Can you try rewording? And what do you mean the errors are expected. – Imirak Aug 06 '12 at 18:13
  • Oh that's easy. Ok so to get the zip code from the array of placemarks, use `valueForKey`. I'll add it in my answer – Imirak Aug 06 '12 at 18:19
  • OK so im confused why your code is giving me errors can you give what you put in the .h? like what is "place"? – user1555256 Aug 06 '12 at 18:25
  • What errors are you getting? I can't tell you what to do if I don't know what's wrong. – Imirak Aug 06 '12 at 18:26
  • ok my errors. undeclared identifier place. the curley brackets are what seems to be giving me errors. – user1555256 Aug 06 '12 at 18:29
  • Oh sorry I forgot one line. Look back at the answer. – Imirak Aug 06 '12 at 18:34
  • Yes, `CLPlacemark *place = [placemarks objectAtIndex:0]` – Imirak Aug 06 '12 at 18:38
  • What do I declare "place" as? – user1555256 Aug 06 '12 at 18:48
  • I just added it, it is of type `CLPlacemark`. – Imirak Aug 06 '12 at 18:53
  • Ok down to 2 errors "expected ]" and "expected }" – user1555256 Aug 06 '12 at 19:05
  • I understand I just don't understand the errors. I have never had this issue before. I think tge issue might have to do with the "dispatch_sync" am I correct? – user1555256 Aug 06 '12 at 19:20
  • Where are you getting your errors? Post how you've used my answer in your code. – Imirak Aug 06 '12 at 19:22
  • Ok I posted my updated code above. sorry i took so long. Thanks! – user1555256 Aug 06 '12 at 23:18
  • I updated my answer. I forgot to close a `)` and a `]` – Imirak Aug 07 '12 at 00:23
  • Add a conditional to make sure that the array placemarks actually contains anything. Take a look at the edit – Imirak Aug 07 '12 at 01:31
  • Do i need CLLocation *aLocation = [[CLLocation alloc] initWithLatitude:latitude longitude:longitude]; and what did you declare longitude as? Thanks! – user1555256 Aug 07 '12 at 01:40
  • That's only if your reverse geocoding some other location. You don't need that if you are just using the current location. Add the conditional in the updated answer. Everything should be working fine. – Imirak Aug 07 '12 at 01:43
  • Ok getting closer! i got an issue that says. "local declaration of 'place' hides instance variable" – user1555256 Aug 07 '12 at 01:51
  • The app Doest seem to be finding the current location. – user1555256 Aug 07 '12 at 01:54
  • That's probably because you have an instance variable called `place` in your header file which coincides with the place variable used for reverse geocoding. So you can just change the name of the `CLPlacemark` variable to like aPlace and then make sure to change it in the zipString assignment – Imirak Aug 07 '12 at 01:55
  • why is my phone not asking me for my current location? – user1555256 Aug 07 '12 at 02:08
  • What do you mean? The answer I've provided should work fine provided you've implemented the `CLLocationManagerDelegate`. Just curious, why did you unaccept my answer? It should work fine. – Imirak Aug 07 '12 at 02:21
  • When i run it my phone is not asking me to use my current location. so i am wondering where i went wrong if my phone isn't even looking for its location? – user1555256 Aug 07 '12 at 02:30
  • Am is supposed to use this CLLocationManager? – user1555256 Aug 07 '12 at 02:32
  • Well how did you get the current location of the phone before? You need the coordinates of your current iPhone location so that you can pass it as a `CLLocation` variable when reverse geocoding. – Imirak Aug 07 '12 at 02:33
  • Ok that what i need help with. If you could help me that would be AWESOME! – user1555256 Aug 07 '12 at 02:36
  • Is This it? - (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark { [geocoder release]; [self performSelectorInBackground:@selector(showWeatherFor:) withObject:[placemark.addressDictionary objectForKey:@"ZIP"]]; } - (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error { NSLog(@"reverseGeocoder:%@ didFailWithError:%@", geocoder, error); [geocoder release]; } – user1555256 Aug 07 '12 at 02:37
  • What do you mean **is that it**? Is that it for what? No, that will not get you the iPhone's current location. Here's a tutorial on getting the iPhone's current location using CoreLocation: http://mobileorchard.com/hello-there-a-corelocation-tutorial/ – Imirak Aug 07 '12 at 02:38
  • i mean is this the code? i know core location just dont know how to intergrade it. – user1555256 Aug 07 '12 at 02:41
  • Ok i will let you know what i get. – user1555256 Aug 07 '12 at 02:46
  • Will the code you gave me work with this http://www.icodeblog.com/2010/09/29/adding-local-weather-conditions-to-your-app-part-22-accessing-googles-xml-weather-api/ that I showed you earlier? – user1555256 Aug 07 '12 at 02:57
  • well thats all i am trying to do and its still not working. – user1555256 Aug 07 '12 at 03:24
  • implanted the tutorial and its still not working can you help. Thanks – user1555256 Aug 07 '12 at 03:28
2

I don't know if this answers your tacit question, but the documentation says to use CLGeocoder instead.

Yusuf X
  • 14,513
  • 5
  • 35
  • 47
  • So do I just replace MKReverseGeocoder with CLGeocoder? and do i add a delegate? – user1555256 Aug 06 '12 at 16:27
  • No, but the answer to this question describes how to update your code: http://stackoverflow.com/questions/7809142/clgeocoder-and-backward-compatibility – Yusuf X Aug 06 '12 at 16:29