Introduction to MapKit in iPhone OS 3.0 Part 2

Back in September I posted a large post going over all the components required to implement the MapKit in your application. The MapKit is a framework introduced in iPhone OS 3.0 and allows developers to easily take advantage of Google’s mapping technology. In my first post I go over how to present a map as well as annotate the map with custom badges to highlight points of interest. The MapKit also gives developers access to reverse geocoding services from Google which I will cover in this post.

Reverse Geocoding

A users location is defined by coordinates. When using the Core Location services, or specifying where a map should center its view, coordinates will be the units developers will be working with. This is all well and good for presenting mapping information visually but there is a whole other set of information that can be derived from a set of coordinates.
  • Country
  • State
  • Zip Code
  • Address
Google provides the service to translate any coordinate set to an MKPlacemark object. An MKPlacemark object contains properties to access all this information. Let’s look into the process of getting this object.

Step 1

The first thing you need to do is make a “View Based Application”. Now we need to is pick a set of coordinates we want to use. I have decided to use the address of Arizona State University.
Longitude: -111.936619;
Latitude: 33.416199;
Make your AppDelegates .m file look like this:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
    // Override point for customization after app launch
    [window addSubview:viewController.view];
    [window makeKeyAndVisible];
 
 CLLocationCoordinate2D coord;
 coord.longitude = -111.936619;
 coord.latitude = 33.416199;
 
 MKReverseGeocoder *geocoder = [[MKReverseGeocoder alloc] initWithCoordinate:coord];
 [geocoder setDelegate:self];
 [geocoder start]
}

Step 2

Now we need to implement the MKReverseGeocoderDelegate methods. There are only 2 delegate methods:
Called when an error occurs when fetching the reverse geocoder object
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error
Called when the ReverseGeocode object is successfully returned.
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark

Step 3

Now we have a MKPlacemark object to use. An MKPlacemark object conforms to the MKAnnotation protocol. So these objects could be added as annotations to an MKMapView. For more information on MKAnnotations reference part 1. The MKPlacemark object contains the following properties that you can access to use in your application.

To see all of these values make the success delegate method look like this:
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark
{
 NSLog(@"The geocoder has returned: %@", [placemark addressDictionary]);
}
You should see this output:
2009-12-22 11:23:05.492 ReverseGeocoder[2591:207] The geocoder has returned: {
    City = Tempe;
    Country = "United States";
    CountryCode = US;
    FormattedAddressLines =     (
        "200-206 E Lemon St",
        "Tempe, AZ 85281",
        USA
    );
    State = Arizona;
    Street = "200-206 E Lemon St";
    SubAdministrativeArea = Maricopa;
    SubThoroughfare = "200-206";
    Thoroughfare = "E Lemon St";
    ZIP = 85281;
}