【问题标题】:How do you read FULL address using coreLocation ( iOS/ Swift )如何使用 coreLocation (iOS/Swift) 读取完整地址
【发布时间】:2016-07-29 03:43:30
【问题描述】:

代码如下:

CLGeocoder().reverseGeocodeLocation(myCurrentLoc) { (array, err) in

        print(err)
        if err != nil {
            print(err) 
            return
        }

        if let place = array?.first {

            if let locality = place.locality {
                if let country = place.country {
                    if let addr = place.addressDictionary {
                        print(addr)
                    }
                    let myAddress = "\(locality) \(country)"
                    self.adress.text = myAddress
                    self.locMgr.stopUpdatingLocation()

                }
            }

        }
        self.locMgr.stopUpdatingLocation()

    }

所以我可以读取经度/纬度,而我唯一需要的是完整地址。现在我可以打印出“New York, US”,我发现我需要的所有信息都在原地。addressDictionary,这是一个字典。但我似乎无法使用正常语法从该字典中读取任何内容。我该怎么做?

【问题讨论】:

    标签: ios swift xcode core-location


    【解决方案1】:

    对于美国地址,您可以使用:

    let address = [
                    place.name, 
                    place.locality,
                    place.administrativeArea,
                    place.postalCode, 
                    place.country
    ]
    
    let formattedAddress = address.flatMap({ $0 }).joined(separator: ", ")
    

    Address 是一组用于组成典型邮政地址的组件。对于某些项目,它可以包含 nil。

    FormattedAddress 去掉 nils 并添加分隔符,我这里用的是逗号。

    【讨论】:

    • [String] 类型的值没有“joined”的成员
    • @FateRiddle 它在​​ Swift 3 中使用。如果您使用较旧的,则有 joinWithSeparator
    【解决方案2】:

    我通常会创建一个位置管理器助手来获取详细信息并使用地标

    你说的字典自带placemarks

    这是我的模型

    class UserLocation: NSObject {
    
     var latitude : String?
     var longitude: String?
     var timeZone: String?
     var city : String?
     var state : String?
     var country : String?
     // you can and more fields of placemark
    
    required init(latitude:String?,longitude:String?,timezone:String?,city:String?,state:String?,country:String?) {
        self.latitude = latitude
        self.longitude = longitude
        self.timeZone = timezone
        self.city = city
        self.state = state
        self.country = country
       }
    
    
    }
    

    然后我在从位置管理器获取位置后使用以下代码反向地理编码并使用placemarks 获取详细信息

    //Location Manager delegate
    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    
        manager.stopUpdatingLocation()
    
        CLGeocoder().reverseGeocodeLocation(manager.location!, completionHandler: {(placemarks, error)->Void in
    
            if (error != nil) {
                print("Reverse geocoder failed with error" + error!.localizedDescription)
                return
            }
            if placemarks!.count > 0 {
                let pm = placemarks![0]
                if let location = manager.location {
                    let userLoc = UserLocation(latitude: String(location.coordinate.latitude), longitude: String(location.coordinate.longitude), timezone: NSTimeZone.localTimeZone().name, city: pm.locality!, state: pm.administrativeArea!, country:pm.country!)
    
                    //By printing this dictionary you will get the complete address
                    print(pm.addressDictionary)
    
                    print(userLoc.city!)
                    print(userLoc.state!)
                    print(userLoc.country!)
                    print(userLoc.latitude!)
                    print(userLoc.longitude!)
                    print(userLoc.timeZone!)
    
                } else {
                    //Handle error
                }
                if(!CLGeocoder().geocoding){
                    CLGeocoder().cancelGeocode()
                }
            }else{
                print("Problem with the data received from geocoder")
            }
    }
    

    您还可以获取地标的其他属性,例如

    我还打印了addressDictionary,您可以在最后一张图片中的控制台中看到完整的地址

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-08-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-09
      • 2016-01-30
      • 2019-10-30
      相关资源
      最近更新 更多