简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM