【问题标题】:CLGeocodeCompletionHandlerCLGeocoder CompletionHandler
【发布时间】:2016-04-02 01:01:14
【问题描述】:

所以我试图让它在完成处理程序之后做一些事情,但我一直收到错误:

预计CLGeocodeCompletionHandler

class func didCompleteGeocoding(placemarks: [CLPlacemark]?, error: NSError?, completion: (results: Bool) -> Void){
   if error == nil && placemarks!.count > 0 {
        let placemark = placemarks![0] as! CLPlacemark
        let geocodedLocation = placemark.location!

   //some more code here
   }
   var geoCoder = CLGeocoder()
   var placemarks: [CLPlacemark]?

   func getlocation(){
   let cordinates = geoCoder.geocodeAddressString(locationtext, completionHandler: toSearchandPost2.didCompleteGeocoding(placemarks, error: nil, completion: { (results) -> Void in
           if results == true{
             // do something here
           }
    }))
}

【问题讨论】:

    标签: ios swift location clgeocoder


    【解决方案1】:

    您不能直接使用该功能。请看下面的代码:

    func getAndDisplayLocationStringForLocation(currentLocation: CLLocation) {
        CLGeocoder().reverseGeocodeLocation(currentLocation, completionHandler: { (placemarks: [AnyObject]!, error: NSError!) in
            if error == nil && placemarks.count > 0 {
                let location = placemarks[0] as CLPlacemark
                self.textField.text = "\(location.locality) \(location.thoroughfare) \(location.subThoroughfare)"
    
            }
        })
    }
    

    你需要这样称呼它:

    yourModel.getAndDisplayLocationStringForLocation(someLocation) { (string: String?, error: NSError?) -> () in
        if (error == nil) {
            self.textField.text = string
        }
    }
    

    请点击以下链接获取完整答案。不过,上面的代码就足够了。

    参考:iOS Swift - CLGeocoder completionHandler block

    【讨论】:

      猜你喜欢
      • 2015-08-10
      • 1970-01-01
      • 1970-01-01
      • 2017-08-04
      • 1970-01-01
      • 2021-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多