ios - 返回类型为空

标签 ios swift xcode

 func getAddressFromLatLon() {



    var locManager = CLLocationManager()
    locManager.requestWhenInUseAuthorization()
    var currentLocation = CLLocation()

    if( CLLocationManager.authorizationStatus() == CLAuthorizationStatus.authorizedWhenInUse ||
        CLLocationManager.authorizationStatus() == CLAuthorizationStatus.authorized){

        currentLocation = locManager.location!

    }




    var center : CLLocationCoordinate2D = CLLocationCoordinate2D()
    let lat: Double = Double(currentLocation.coordinate.latitude)
    //21.228124
    let lon: Double = Double(currentLocation.coordinate.longitude)
    //72.833770
    let ceo: CLGeocoder = CLGeocoder()
    center.latitude = lat
    center.longitude = lon

    let geoCoder = CLGeocoder()
    var placemark: AnyObject
    var error: NSError
    geoCoder.reverseGeocodeLocation(locManager.location!, completionHandler: { (placemark, error) -> Void in
        if error != nil {
            print("Error: \(error?.localizedDescription)")
            return
        }
        if (placemark?.count)! > 0 {
            let pm = (placemark?[0])! as CLPlacemark
            //self.addressString = pm.locality! + pm.country!
            let adress = pm.locality! + " " + pm.country!

            print(adress)
        } else {
            print("Error with data")
        }
    })



}

对这个非常基本的问题感到抱歉。我对 swift 相当陌生,我正在尝试对纬度和经度进行反向地理编码。我试图从反向地理编码返回地址,但它似乎返回零。有问题的函数是 getAddressFromLatLon() ,我尝试添加返回类型,但它返回 nil。当我在函数本身中打印时,会打印正确的值,但由于某种原因,我很难返回地址,因此我可以将其传递给其他类/函数。

最佳答案

您需要保存从保留地理编码器获得的值并且只需要使用它。

  geoCoder.reverseGeocodeLocation(locManager.location!, completionHandler: { (placemark, error) -> Void in
            if error != nil {
                print("Error: \(error?.localizedDescription)")
                return
            }
            if (placemark?.count)! > 0 {
                let pm = (placemark?[0])! as CLPlacemark
                //self.addressString = pm.locality! + pm.country!
                let adress = pm.locality! + " " + pm.country!

                // Store value into global object and you can use it...
               // Another option, you can call one function and do necessary steps in it
                mainLocality = pm.locality
                mainCountry - pm.country       
                updateGeoLocation() // Call funcation

                print(adress)
 } else {
            print("Error with data")
        }
    })


func updateGeoLocation(){
     // You can use both object at here
     //    mainLocality
     //    mainCountry
     // Do your stuff

}

关于ios - 返回类型为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44492723/

相关文章:

ios - 从 Jenkins 中获取 CFBundleVersion 以将其用作变量,例如 ${APP_VERSION}

swift - 为什么我的自定义类没有出现在 Interface Builder 的下拉列表中?

ios - Apple 的 9 位应用程序 ID

JavaScriptCore - 在 iOS 中访问 DOMParser

ios - 访问 AddressBook 是否需要征求用户的许可?

c++ - 在 OSX 上使用 CMake 构建 Unity native 包

ios - Storyboard 中的错误

swift - '#selector' 的参数不引用 Swift 按钮目标中的 '@objc' 方法、属性或初始值设定项

ios - XLForm - 如何左对齐单元格?

ios - 如何将函数传递给 Swift 中的另一个类