Convert NSString Latiude/Longitude Coordinates to City, State, and Timezone with CLGeocoder
ios, iphone, objective-c, xcode
Solution
A couple things, Brandon:
1) CLLocationManager might not give you an instant response to your request for coordinates. You should set your view controller as a CLLocationManager delegate and then when the location update comes in (which will be in the `locationManager:didUpdateLocations:` method), then you can run your CLGeocoder method.
2)
Which I wrote to look like this:
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
NSLog( @"didUpdateLocation!");
NSLog( @"latitude is %@ and longitude is %@", [self getUserLatitude], [self getUserLongitude]);
CLGeocoder * geoCoder = [[CLGeocoder alloc] init];
[geoCoder reverseGeocodeLocation:locationManager.location completionHandler:^(NSArray *placemarks, NSError *error) {
for (CLPlacemark * placemark in placemarks) {
NSString * addressName = [placemark name];
NSString * city = [placemark locality]; // locality means "city"
NSString * administrativeArea = [placemark administrativeArea]; // which is "state" in the U.S.A.
NSLog( @"name is %@ and locality is %@ and administrative area is %@", addressName, city, administrativeArea );
}
}];
}
Getting the location's timezone is a bit trickier. I bet there's an API or some sample code to get it within iOS, but it's not a part of the CLPlacemark API.
Problem
I have a view controller that pulls the users latitude and longitude coordinates from the app delegate. This works well, but I also need the user's city, state, and time zone. I know I should use CLGeocoder for this (please see last chunk of code), but don't know how to put it together. I'd just need NSStrings of the city, state, and timezone. Anyone have any pointers or an example? Thank you! In my App Delegate, I use CCLocationManager to get the Coordinates like this: ``` - (NSString *)getUserCoordinates { NSString *userCoordinates = [NSString stringWithFormat:@"latitude: %f longitude: %f", locationManager.location.coordinate.latitude, locationManager.location.coordinate.longitude]; locationManager = [[CLLocationManager alloc] init]; locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m [locationManager startUpdatingLocation]; return userCoordinates; } - (NSString *)getUserLatitude { NSString *userLatitude = [NSString stringWithFormat:@"%f", locationManager.location.coordinate.latitude]; return userLatitude; } - (NSString *)getUserLongitude { NSString *userLongitude = [NSString stringWithFormat:@"%f", locationManager.location.coordinate.longitude]; return userLongitude; } ``` In my View Controller, I get the user's Latitude and Longitude as an NSString with this: ``` NSString *userLatitude =[(PDCAppDelegate *)[UIApplication sharedApplication].delegate getUserLatitude]; NSString *userLongitude =[(PDCAppDelegate *)[UIApplication sharedApplication].delegate getUserLongitude]; ``` I would like to get the city, state, and timezone. I understand I need CLGeocoder, but can't figure out how to meld it together: ``` CLGeocoder * geoCoder = [[CLGeocoder alloc] init]; [geoCoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error) { for (CLPlacemark * placemark in placemarks) { NSString *locality = [placemark locality]; } } ```