pjtnt11
pjtnt11

Reputation: 522

How to show the current state you are located in when using reverseGeocodeLocation in swift

I'm testing out reverseGeocodeLocation with this app that shows your closest address. I've gotten everything to work except for the showing of the current state that you are in (IL, NY, ext.). How do I do that? This is my current code:

CLGeocoder().reverseGeocodeLocation(userLocation)
    { (placemarks, error) -> Void in

        if error != nil
        {
            println(error)
        }
        else
        {
            let pm = CLPlacemark(placemark: placemarks![0] as CLPlacemark)

            var subThoroughtare:String = ""
            var thoroughfare:String = ""
            var subLocality:String = ""
            var subAdministrativeArea:String = ""
            var postalCode:String = ""
            var country:String = ""

            if pm.subThoroughfare != nil {subThoroughtare = pm.subThoroughfare!}
            if pm.thoroughfare != nil {thoroughfare = pm.thoroughfare!}
            if pm.subLocality != nil {subLocality = pm.subLocality!}
            if pm.subAdministrativeArea != nil {subAdministrativeArea = pm.subAdministrativeArea!}
            if pm.postalCode != nil {postalCode = pm.postalCode!}
            if pm.country != nil {country = pm.country!}
            self.addressLabel.text = "\(subThoroughtare) \(thoroughfare) \n \(subLocality) \n \(postalCode) \n \(country)"
        }
    }

and the output is this (example location):

 County Road 1760 

 79529 
 United States

Upvotes: 4

Views: 1665

Answers (1)

Rich Fox
Rich Fox

Reputation: 2194

for the state you want to look at the administrativeArea

    let state = pm.administrativeArea;

If you look at the definition for the CLPlacemark class it shows..

var administrativeArea: String! { get } // state, eg. CA

Upvotes: 6

Related Questions