ios - 为什么将 subLocality 与反向GeocodeLocation 结合使用时,当前的 subLocality 并不总是弹出

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

最佳答案

如果目标是让城市使用地点代替

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

区别在于subLocality实际上是该位置的社区/通用名称。

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

关于ios - 为什么将 subLocality 与反向GeocodeLocation 结合使用时,当前的 subLocality 并不总是弹出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31460551/

相关文章:

ios - 使用 CocoaPods 安装 MapBoxNavigation 后出现 Swift 编译器错误问题

ios - Swift/ios/Google map - 添加 UISlider 时出现 "Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value"

ios - 使用 RxSwift 的 TabelView 委托(delegate)方法

ios - 协调器模式 - 使用 Storyboard 而不是 Xib

ios - 创建具有透明背景 Swift 的模态视图

ios - RxSwift 重新加载表格 View

ios - 从服务器格式化文本

ios - iOS 中的自动续订应用内购买

ios - 我如何调用这个复杂的 NSURLSession?

iphone - UINavigationController 后退按钮位置