0

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];

 }];
mreynol
  • 309
  • 3
  • 17

2 Answers2

0

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.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Thank you Matt. I'm working on going thru docs that i can find which I had spent some time doing already. I'm struggling with how to parse out as i cannot find any sample code. i did see the properties on the apple site but that is only part of the picture it seems. Still searching though... – mreynol Dec 01 '12 at 04:56
  • You don't need "sample code". You've got the placemark, so just log its various properties or look at it in the debugger. Similarly, just NSLog the dictionary or look at it in the debugger. You'll see immediately how it's constructed. You are a programmer, don't wait for everything to be handed to you on a plate. – matt Dec 01 '12 at 05:30
  • I do appreciate your help though Matt. – mreynol Dec 01 '12 at 05:40
-1
// Reverse Geocoding
NSLog(@"Resolving the Address");
[_geoCoder2 reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error) {
    NSLog(@"Found placemarks: %@, error: %@", placemarks, error);
    if (error == nil && [placemarks count] > 0) {
        placemark = [placemarks lastObject];
        _cityLabel.text = [NSString stringWithFormat:@"%@\n%@\n",

                           placemark.locality,
                           placemark.administrativeArea];

    } else {
        NSLog(@"%@", error.debugDescription);
    }
} ];
mreynol
  • 309
  • 3
  • 17
  • But that is exactly what I said. Instead of accepting my answer, you've repeated it as your own answer. – matt Dec 01 '12 at 20:19
  • Because there are different kinds of people who need help. I happen to need to see code that i can pick apart and learn from along with reading documentation. There is no need to reinvent the wheel when it comes to lot of this stuff. People will spend hours going thru documentation when they learn just as much obtaining existing code and learning from it and adjusting to fit their needs. Dock me all you want, but i felt providing the code is more helpful to people. Sorry if you take offense to that. – mreynol Dec 01 '12 at 23:34
  • My answer explicitly told you about the CLPlacemark locality and administrativeArea properties. You turn around and create code in which you simply read the placemark.locality and placemark.administrativeArea properties. Yet you don't credit my answer which told you those were the properties you needed. You said you wanted just city and state, I told you how to get them. – matt Dec 01 '12 at 23:56
  • I merely provided the code as a second answer. I didn't accept either one and you berate me. I told you I appreciated your help and you also belittle me by telling me, someone you don't know, to not wait for things to be handed to me on a platter. Since it seems to mean so much to your 10,742 rating, i have Accepted your answer. Have a great day. – mreynol Dec 02 '12 at 00:28