相关疑难解决方法(0)

Swift - 从反向地理编码生成地址格式

我试图使用Swift 3中的CLGeocoder生成格式化的完整地址.我引用了这个SO线程来获取下面给出的代码.

但是,有时应用程序崩溃时会出现"nil"错误:

//Address dictionary
print(placeMark.addressDictionary ?? "")
Run Code Online (Sandbox Code Playgroud)

问题:

  1. 如何连接从GeoCoder检索到的这些值以形成完整地址?(街+城+等)
  2. 当func无法找到地址时,如何处理我得到的nil错误?

完整代码:

func getAddress() -> String {
        var address: String = ""

        let geoCoder = CLGeocoder()
        let location = CLLocation(latitude: selectedLat, longitude: selectedLon)
        //selectedLat and selectedLon are double values set by the app in a previous process

        geoCoder.reverseGeocodeLocation(location, completionHandler: { (placemarks, error) -> Void in

            // Place details
            var placeMark: CLPlacemark!
            placeMark = placemarks?[0]

            // Address dictionary
            //print(placeMark.addressDictionary ?? "")

            // Location name
            if let locationName = placeMark.addressDictionary!["Name"] …
Run Code Online (Sandbox Code Playgroud)

xcode google-maps reverse-geocoding ios swift

20
推荐指数
4
解决办法
4万
查看次数

标签 统计

google-maps ×1

ios ×1

reverse-geocoding ×1

swift ×1

xcode ×1