O'Reilly logo

iPad Application Development For Dummies, 3rd Edition by Neal Goldstein

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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) ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required