O'Reilly logo

iOS 5 Programming Cookbook by Vandad Nahavandipoor

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

6.8. Converting Longitude and Latitude to a Meaningful Address

Problem

You have the latitude and longitude of a spatial location and you want to retrieve the address of this location.

Solution

Retrieving a meaningful address using spatial x and y coordinates is called reverse geocoding. To do this, create and use an instance of the CLGeocoder class and provide a completion block object, making sure that the block object has no return value and accepts two parameters:

  1. A placemarks array (of type NSArray), which will be set to the locations which matched your search.

  2. An error (of type NSError), which will get set to an error code if the reverse geocoding fails.

After instantiating an object of type CLGeocoder, we will use its reverseGeocodeLocation:completionHandler: method to do the reverse geocoding.

The .h file of a simple view controller for this purpose is defined like so:

#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>

@interface
  Converting_Longitude_and_Latitude_to_a_Meaningful_AddressViewController
   : UIViewController

@property (nonatomic, strong) CLGeocoder *myGeocoder;

@end

Now synthesize your myGeocoder property:

@implementation
  Converting_Longitude_and_Latitude_to_a_Meaningful_AddressViewController

@synthesize myGeocoder;

...

You can do the reverse geocoding when your view loads:

- (void)viewDidLoad{ [super viewDidLoad]; CLLocation *location = [[CLLocation alloc] initWithLatitude:+38.4112810 longitude:-122.8409780f]; self.myGeocoder = [[CLGeocoder alloc] init]; [self.myGeocoder ...

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