【问题标题】:Return Type Nill返回类型 无
【发布时间】:2017-06-12 06:31:21
【问题描述】:
 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。当我在函数本身中打印时,会打印正确的值,但由于某种原因,我很难让地址返回,因此我可以将它传递给其他类/函数。

【问题讨论】:

  • func getAddressFromLatLon() 是一个无效函数。你不能指望它返回任何值。
  • 我还想确认一件事,print(adress) 是否显示了想要的结果?..
  • 你在这个函数中得到了 lat long 吗?

标签: ios swift xcode


【解决方案1】:

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

  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

}

【讨论】:

    【解决方案2】:

    您需要在 ViewDidLoad 中初始化 CLLocation 对象并在其委托方法中获取位置坐标

    声明当前位置的变量

    var currentLocation : CLLocation
    

    在 ViewDidLoad 中

     var locManager = CLLocationManager()
    
        locationManager.delegate = self;
                locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
                // ask permission - NOT NECESSARY IF YOU ALREADY ADDED NSLocationAlwaysUsageDescription IT UP INFO.PLIST
                locationManager.requestAlwaysAuthorization()
                // when in use foreground
                locationManager.requestWhenInUseAuthorization()
                locationManager.startUpdatingLocation()
    

    CLLocation 的代表

     func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    
            // Get first location item returned from locations array
            currentLocation = locations[0]
            self.getAddressFromLatLon() // you can call here or whenever you want
        }
    

    您的方法如下

    func getAddressFromLatLon() {
    
        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")
            }
        })
    
    
    
    }
    

    【讨论】:

      猜你喜欢
      • 2012-02-07
      • 2021-02-18
      • 2023-04-03
      • 2019-05-07
      • 2013-02-26
      • 1970-01-01
      • 1970-01-01
      • 2022-12-06
      相关资源
      最近更新 更多