10

I've been all over the internet trying to find out how to get the city and country from CLGeocoder. I can get the longitude and latitude easily but I need the city and country information, and I keep running into deprecated methods and such, any ideas? It basically needs to get the location, then have an NSString for the country, and an NSString for the city, so I can use them to look up more info or put them on labels, etc.

Carl Veazey
  • 18,392
  • 8
  • 66
  • 81
Jon Sullivan
  • 399
  • 2
  • 5
  • 11

2 Answers2

18

You need to revise your terminology a bit - CLGeocoder (and most geocoders) won't give you a 'city' per-se - it uses terms such as 'Administrative Area', 'Subadministrative Area', etc. The CLGeocoder object will return an array of CLPlacemark objects which you can then query for the information you need. You init a CLGeocoder and call the reverseGeocodeLocation function with a location and a completion block. Here's an example:

    if (osVersion() >= 5.0){

    CLGeocoder *reverseGeocoder = [[CLGeocoder alloc] init];

    [reverseGeocoder reverseGeocodeLocation:self.currentLocation completionHandler:^(NSArray *placemarks, NSError *error)
     {
         DDLogVerbose(@"reverseGeocodeLocation:completionHandler: Completion Handler called!");
         if (error){
             DDLogError(@"Geocode failed with error: %@", error);
             return;
         }

         DDLogVerbose(@"Received placemarks: %@", placemarks);


         CLPlacemark *myPlacemark = [placemarks objectAtIndex:0];
         NSString *countryCode = myPlacemark.ISOcountryCode;
         NSString *countryName = myPlacemark.country;
         DDLogVerbose(@"My country code: %@ and countryName: %@", countryCode, countryName);

     }];
    }

Now note that the CLPlacemark doesn't have a 'city' property. The full list of properties can be found here: CLPlacemark Class Reference

Jai Govindani
  • 3,181
  • 21
  • 26
  • 6
    Great answer. To clarify, you can get the city name by accessing the "City" key within the `addressDictionary` property of the `CLPlacemark *myPlacemark`object: i.e. `myPlacemark.addressDictionary[@"City"]` but that should be apparent after reading the docs as you mentioned :) – manderson Dec 16 '13 at 16:42
  • Hey cool thanks for that - yeah just looked and pretty nifty, follows the fields of the address book. There's a cool method for piecing together the full address too. Thanks! – Jai Govindani Dec 17 '13 at 03:16
  • Thanks It is a good code for getting the city name, using CLGeocoder, very easy and quick to implement... – Arpit B Parekh Jul 02 '15 at 09:41
  • The city associated with the placemark is under the "locality" key. – halbano Nov 03 '16 at 16:02
0

You can get city, country and iso country code using this (Swift 5):

private func getAddress(from coordinates: CLLocation) {
    CLGeocoder().reverseGeocodeLocation(coordinates) { placemark, error in
        guard error == nil,
            let placemark = placemark
        else
        {
            // TODO: Handle error
            return
        }

        if placemark.count > 0 {
            let place = placemark[0]
            let city = place.locality
            let country = place.country
            let countryIsoCode = place.isoCountryCode
        }
    }
}
IvanPavliuk
  • 1,460
  • 1
  • 21
  • 16