【问题标题】:How to get borough from UK postcode using CoreLocation in swift?如何在 swift 中使用 CoreLocation 从英国邮政编码获取自治市镇?
【发布时间】:2018-03-11 15:34:52
【问题描述】:

有谁知道我是否可以使用 CoreLocation 框架从英国邮政编码中获取自治市镇?在下面列出的函数中,不返回自治市镇。它应该返回 Tower Hamlets。

  func getLocationFrom(postalCode : String){
    let geocoder = CLGeocoder()

    geocoder.geocodeAddressString(postalCode) {
        (placemarks, error) -> Void in
        let placemark = placemarks?[0] 
             print("placemark.addressDictionary are \(placemark.addressDictionary)") //does not print Canary Wharf  
    }
}

print(getLocationFrom(postalCode:"E14 9LR")) //does not print Tower Hamlets, it prints London, which is the city, not the borough..

【问题讨论】:

    标签: swift core-location


    【解决方案1】:

    为了从邮政编码中获取位置,您只需为 placemarks 数组下标,然后访问所需 indexPath 处元素的属性 locality。我的错误是我试图访问placemarks 数组上的另一个属性,即addressDictionary,它没有产生预期的结果。

     func getLocationFrom(postalCode: String, callback: @escaping(_ locality: String?) -> Void){
    
    
        let geocoder = CLGeocoder()
        geocoder.geocodeAddressString(postalCode) {
            (placemarks, error) -> Void in
    
           callback(placemarks?[0].locality)
    
        }
    

    【讨论】:

    • 回答您自己的问题很好,但请不要只发布代码。解释原始代码有什么问题,并解释这个答案如何解决问题。
    • 原来我错了,我仍然没有得到邮政编码输入的自治市镇。请参阅我修改后的问题。相反,我得到了城市..
    猜你喜欢
    • 1970-01-01
    • 2017-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多