CLGeocoder example

Reverse Geocoding has been achieved using MKReverseGeocoder, however in the latest version of iOS, iOS 5.0 it has been deprecated, this does not mean that you cannot use it but it does mean that its use is not actively encouraged. Chances are that if the application you are building is targetting for devices running pre iOS5 operating systems then it will be approved. However if you are building for iOS 5 and onwards then there is a possiblilty that you're application will not be approved as you are using a deprecated method or so I have heard.  In the meantime Apple recommend you use CLGeocoder instead.

So what is Reverse Geocoding anyway?

Well according to Wikipedia:

Reverse geocoding is the process of back (reverse) coding of a point location (latitude, longitude) to a readable address or place name. This permits the identification of nearby street addresses, places, and/or areal subdivisions such as neighbourhoods, county, state, or country.

That definition is very accurate, basically in our applications we want to be able to convert the reading provided by the GPS unit usually delivered in the form of latitude and longitude into something that is readable by regular humans, im not saying that longitude and latitude are not useful im just saying that different apps require different results. When I was writing my first iOS application Location+ I wanted to add some Reverse Geocoding functionality, but I was targetting iOS5 and onwards I wasnt going to be able to use MKReverseGeocoder. This was a bit irritating at the start as being a self taught and very much still learning Objective-C/iOS programmer I rely heavily on tutorials posted on the web for both technical and inspirational help and with CLGeocoder being so new and also most people still using MKReverseGeocoder there was not much available to help really. Hopefully this tutorial will add some more content.

So what does it really look like?

iOS Simulator Screen shot 4 Dec 2011 22.07.57

This is a screenshot taken from the simulator in Xcode that shows the basic information that can be retrieved using CLGeocoder and im sure that you can see immediately that it can be very useful to a whole plethora of App developers. Chances are if you are reading this you are probably just learning the language and might well be just creating a location based application where you can feel like you have achieved something very quickly.

What information is made available by CLGeocoder?

Using the CLGeocoder to return a CLPlacemark is the way that I have done it, a CLPlacemark object is bascially just a marker for your current locaton that contains information about where you are, its as simple as that.

Screen Shot 2011-12-05 at 09.56.34

As you can see there is a lot of possible values that you can extract from a placemark but they are named a little vaguely so once you have it working you will have to experiment a little bit to get what you are after.

How do I do it?

Screen Shot 2011-12-05 at 09.59.38

Well its actually suprisingly simple, firstly create yourself a few labels or text boxes and then add them to your header file to have the synthesizing done for you. You can name them with something meaningful e.g. townLabel or something along those lines. Next step is to create a CLGeocoder and LocationManager objects and repeat the same steps, in this case I called mine CLGeocoder. Note you may need to add MapKit and CoreLocation to your project, just add them in the bundled libraries and import the header files <MapKit/MapKit.h> and <CoreLocation/CoreLocation.h> if you have any problems. After that switch to your implementation file (your .m file) then either create a simple -(IBAction) method attached to a button or place this code in the viewDidLoad function. You can comment out the [locationLabel setText] method if you dont have a label and it will just be printed to the console.

  //Geocoding Block

    [self.geoCoder reverseGeocodeLocation: locationManager.location completionHandler

     ^(NSArray *placemarks, NSError *error) {


         //Get nearby address

         CLPlacemark *placemark = [placemarks objectAtIndex:0];


         //String to hold address

         NSString *locatedAt = [[placemark.addressDictionary valueForKey:@"FormattedAddressLines"] componentsJoinedByString:@", "];

         //Print the location to console

         NSLog(@"I am currently at %@",locatedAt);


         //Set the label text to current location

         [locationLabel setText:locatedAt];




Basically what this peice of code does is enable the LocationManager object so that the phone tracks where you are and then Reverse Geocoder your location based on the LocationManager parameter that you pass it, in this case we have used locationManager.location. Apple say that you should not call Reverse Geocoding methods more than once a minute so the best way for us to do this is just have have the function calls in the viewDidLoad so they will be ready for when the view loads and are only called once each time the view is loaded. To actually get the data you need to call the reverseGeocodeLocation function, this returns an Array of CLPlacemarks, in this case we only need the information once so we will be accessing index 0 of the Array. By accessing Index 0 we create a placemark object that can easily be assigned to a text field by calling               yourtextbox.text = placemark.whatever; its really as easy as that.

This may be a bit vague but everything that you need is here to help allow you to use the CLGeocoder in your applications and perhaps it will be of help to a few people.

Good Luck!

© Jonathan Field 2011