【问题标题】:Unable to get state & zipcode with CLGeoCoder in swift?无法使用 CLGeoCoder 快速获取州和邮政编码?
【发布时间】:2017-11-20 21:36:30
【问题描述】:

我无法使用 CLGeocoder 获取州和邮政编码。我只获取位置名称、国家和城市。我在印度使用此代码获取位置。我为此使用以下代码。

func getAddressFromLocation(location: CLLocation) {

        CLGeocoder().reverseGeocodeLocation(location, completionHandler: { (placemarks, error) -> Void in
            print(location)
            self.activityIndicator.stopAnimating()
            if error != nil {
                DILog.print(items: "Reverse geocoder failed with error" + (error.debugDescription))
                return
            }

            var placeMark: CLPlacemark!
            placeMark = placemarks?[0]

            if placeMark == nil {
                return
            }

            // Location name
            if let locationName = placeMark.addressDictionary!["Name"] as? String {
                self.currentLocation = self.currentLocation + locationName
            }

            // Street address
            if let street = placeMark.addressDictionary!["Thoroughfare"] as? String {
                self.currentLocation = self.currentLocation + ", " + street
                self.spotLocation.street_address_1 = street

            }

            // City
            if let city = placeMark.addressDictionary!["City"] as? String {
                print(city, terminator: "")
                self.currentLocation = self.currentLocation + ", " + city
                self.spotLocation.city = city
            }

            // Country
            if let country = placeMark.addressDictionary!["Country"] as? String {
                print(country, terminator: "")
                self.currentLocation = self.currentLocation + ", " + country
                self.spotLocation.country = country

            }
            // Street address
            if let street1 = placeMark.addressDictionary!["SubThoroughfare"] as? String {
                self.currentLocation = self.currentLocation + ", " + street1
                self.spotLocation.street_address_1 = street1


            }
            // state
            if let state = placeMark.addressDictionary!["State"] as? String {
                self.currentLocation = self.currentLocation + ", " + state
                self.spotLocation.state = state
            }
            // Zip
            if let zip = placeMark.addressDictionary!["Zip"] as? String {
                self.currentLocation = self.currentLocation + ", " + zip
                self.spotLocation.state = zip
            }
            self.locationLabel.text? = self.currentLocation


        })

    }

请告诉我如何获得解决方案?

【问题讨论】:

  • printaddressDictionary 看看它包含什么吗?为什么将placeMark 声明为隐式展开可选?如果 optional(!) 数组 placemarks 不是 nil 并且它包含一个项目,则该项目定义为 非可选。但是,如果数组为空,您的代码将崩溃。
  • 我添加了检查地标是否为零然后返回
  • 如果placemarks?[0] 成功,placemark永远成为nil。但是,如果数组为空,代码会在 placemarks?[0] 行中崩溃。检查nil(和IUO声明)是没有意义的。
  • 我会在那里添加支票。感谢您的重要评论。现在你能告诉我我能得到邮政编码和州吗
  • 再一次:你有没有 print addressDictionary 看看它包含什么。它是否包含StateZip 键?如果是,值是什么类型?

标签: ios swift clgeocoder


【解决方案1】:
let zipCode = placeMark.postalCode ?? "unknown"

【讨论】:

    猜你喜欢
    • 2019-08-02
    • 2013-04-05
    • 1970-01-01
    • 1970-01-01
    • 2010-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多