当使用带有reverseGeocodeLocation

时间:2015-07-16 17:12:43

标签: ios swift

我正在创建一个应用程序,告诉用户他们最近的地址是什么。如果subLocality几乎总是不可用,那么接缝就成了问题。我相信subLocality基本上就是你所在的城市,我已经看到这样的例子显示得很好。有什么问题?这是我的代码:

    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)"
        }
    }

1 个答案:

答案 0 :(得分:0)

如果目标是让城市使用位置而不是

var locality: String! { get } // city, eg. Cupertino

区别在于 subLocality 实际上是该位置的邻域/公用名。

var subLocality: String! { get } // neighborhood, common name, eg. Mission District