Skip to content

Instantly share code, notes, and snippets.

@jungchris
Created April 17, 2015 22:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jungchris/f40a5eb1827cedb0e1cd to your computer and use it in GitHub Desktop.
Save jungchris/f40a5eb1827cedb0e1cd to your computer and use it in GitHub Desktop.
Reverse GeoCoder using CLLocationManager
// didUpdateToLocation is deprecated, replaced with didUpdateToLocations with an array
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
// code in case I need to get more than one location
int objCount = [locations count];
NSLog(@"MapVC: Delegate: didUpdateLocationS - objects count = %u", objCount);
if (objCount > 1) {
CLLocation *oldLocation = [locations objectAtIndex:objCount - 1];
NSLog(@"Prior location %f,%f", oldLocation.coordinate.latitude, oldLocation.coordinate.longitude);
}
CLLocation *newLocation = [locations lastObject];
self.currentLocation = newLocation;
if(newLocation.horizontalAccuracy <= 1000.0f){
[self.locationManager stopUpdatingLocation];
}
CLGeocoder * geoCoder = [[CLGeocoder alloc] init];
[geoCoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error) {
if (error){
NSLog(@"Geocode failed with error: %@", error);
return;
}
NSString * location = ([placemarks count] > 0) ? [[placemarks objectAtIndex:0] locality] : @"Not Found";
NSLog(@"=> reverseGeo Location: %@", location);
// get the CLPlacemark object created by the CLGeocoder
CLPlacemark *myPlacemark = [placemarks objectAtIndex:0];
NSString *adminArea = myPlacemark.administrativeArea;
NSString *countryCode = myPlacemark.ISOcountryCode;
NSString *countryName = myPlacemark.country;
NSString *subAdmin = myPlacemark.subAdministrativeArea;
NSString *city = myPlacemark.locality;
NSLog(@"My country code: %@ and countryName: %@ and adminArea: %@ and subAdmin: %@ andCity: %@", countryCode, countryName, adminArea, subAdmin, city);
// call custom helper method to determine which row of the state array we are working with
[self setupFromReverseGeocodedLocation:adminArea];
}];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment