anyone know how to split out like city, state and address from this code? It returns the entire address, but i only want city and state.
//Geocoding Block
[_geoCoder2 reverseGeocodeLocation: _currentLocation2.location completionHandler:
^(NSArray *placemarks, NSError *error) {
//Get nearby address
CLPlacemark *placemark = [placemarks objectAtIndex:0];
//String to hold address
NSString *locatedAt = [[placemark.addressDictionary valueForKey:@"FormattedAddressLines"] componentsJoinedByString:@", "];
//Print the location to console
NSLog(@"I am currently at %@",locatedAt);
//Set the label text to current location
[_cityLabel setText:locatedAt];
}];
CLPlacemark has properties such as locality
and administrativeArea
. See the docs to learn what they are, but you'll want to experiment to see how they parse out the address into its components. Also, the addressDictionary
is in address book format, so there are keys for city and state inside it; your mistake is turning it into a string rather than examining the structure of the dictionary.