Skip to content

Reverse Geocode in iOS w/ CLGeocoder

Screen Shot 2014-01-07 at 4.16.22 PM

If you’re using CLLocationManager to obtain the user’s location, you can add some code in the delegate method to reverse geocode the user’s location…

- (void)locationManager:(CLLocationManager *)manager
	didUpdateToLocation:(CLLocation *)newLocation
		   fromLocation:(CLLocation *)oldLocation 
{
    if (curLocation.horizontalAccuracy < 200) // w/in 200 meters
    {
        [locMgr stopUpdatingLocation];
        NSLog(@"Location: %f %f", curLocation.coordinate.latitude, 
          curLocation.coordinate.longitude);

        // find a placemark using the found coordinates
        CLGeocoder *geoCoder = [[CLGeocoder alloc] init];
        [geoCoder reverseGeocodeLocation:newLocation 
          completionHandler:^(NSArray *placemarks, NSError *error) {
            if (error)
                NSLog(@"Error: %@", error);
            else if (placemarks.count > 0)
                curPlacemark = [placemarks firstObject];
        }];
    }
}

Alloc-init a CLGeocoder and call reverseGeocodeLocation: w/ the location passed in (or any location) and a completion handler.

I’m storing it in a instance variable to use later. Here’s some code to print out the details…

NSString *addressTxt = [NSString stringWithFormat:@"%@ %@,%@ %@",
  [topResult subThoroughfare],[topResult thoroughfare],
  [topResult locality], [topResult administrativeArea]];
NSLog(@"result: %@",addressTxt);
NSLog(@"coordinate: %f %f", curPlacemark.location.coordinate.latitude, 
  curPlacemark.location.coordinate.longitude);

It’s fairly simple. I’ll look at forward geocoding soon.

1 thought on “Reverse Geocode in iOS w/ CLGeocoder”

  1. Pingback: Forward Geocode in iOS with CLGeocoder | Brainwash Inc.

Comments are closed.