Reverse GeoCoder using CLLocationManager
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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