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
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
- (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]); }
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; }