Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CLGeocoder and backward compatibility

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!

like image 722
Mathieu Mahé Avatar asked Oct 18 '11 14:10

Mathieu Mahé


3 Answers

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;

}];
like image 169
Jonathan Field Avatar answered Nov 06 '22 14:11

Jonathan Field


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

like image 28
justin Avatar answered Nov 06 '22 14:11

justin


- (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

like image 1
krishnendra Avatar answered Nov 06 '22 14:11

krishnendra