Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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