Reverse Geocoding

You’ll begin the process of implementing reverse geocoding by adding a new instance variable to MapController.m to store a reference to the CLGeocoder object. You do all this by adding the bolded code in Listing 18-1 to MapController.m. As you’ll see later, you’ll need that reference to cancel a request.

Listing 18-1: Updating the MapController Implementation

#import “MapController.h”

#import “RTAppDelegate.h”

#import “Trip.h”

@interface MapController () {

CLGeocoder* geocoder;

}

@end

Next, you allocate and initialize the CLGeocoder and send it a message to return the information for the current location. Adding the bolded code in Listing 18-2 to goToLocation in MapController.m does that for you.

Listing 18-2: Updating goToLocation

- (void)goToLocation:(id)sender {

void (^clGeocodeCompletionHandler)(NSArray *, NSError *) =

^(NSArray *placemarks, NSError *error){

CLPlacemark *placemark = [placemarks objectAtIndex:0];

if (error!= nil || placemark == nil) {

NSLog(@”Geocoder failure! Error code: %u,

description: %@, and reason: %@”, error.code, [error localizedDescription],

[error localizedFailureReason]);

}

else {

mapView.userLocation.subtitle =

[NSString stringWithFormat: @” lat:%f lon:%f”, placemark.location.coordinate.latitude,

placemark.location.coordinate.longitude];

if ([placemark.areasOfInterest objectAtIndex:0]) {

mapView.userLocation.title =

[placemark.areasOfInterest objectAtIndex:0];

}

else {

if (placemark.thoroughfare) ...

Get iPad Application Development For Dummies, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.