Swift 2.0 GeoCoder 参数列表

标签 swift closures geocoding swift2

花了一天时间将项目转换为 Swift 2.0,我对以下代码片段的问题感到困惑,该代码片段旨在在 map 上显示地址(以字符串格式提供)。

geocoder.geocodeAddressString(addressString, completionHandler: {(placemarks: [AnyObject]!, error: NSError!) -> Void in
    if(error != nil) {

        println("Error", error)
    } else if let placemark = placemarks?[0] as? CLPlacemark {

        var placemark:CLPlacemark = placemarks[0] as! CLPlacemark
        let coordinates:CLLocationCoordinate2D = placemark.location.coordinate
        let span:MKCoordinateSpan = MKCoordinateSpanMake(0.01 , 0.01)
        let region:MKCoordinateRegion = MKCoordinateRegionMake(coordinates, span)

        var pointAnnotation:MKPointAnnotation = MKPointAnnotation()
        pointAnnotation.coordinate = coordinates
        pointAnnotation.title = locationTitle
        pointAnnotation.subtitle = addressString

        map.addAnnotation(pointAnnotation)
        map.centerCoordinate = coordinates
        map.setRegion(region, animated: true)
        map.selectAnnotation(pointAnnotation, animated: true)
    }
})

最佳答案

这更接近了。函数签名已更改,println 现在是 print:

geocoder.geocodeAddressString(addressString, completionHandler: {(placemarks: [CLPlacemark]?, error: NSError?) -> Void in
    if (error != nil) {
        print("Error \(error!)")
    } else if let placemark = placemarks?[0] {

        let coordinates:CLLocationCoordinate2D = placemark.location.coordinate
        let span:MKCoordinateSpan = MKCoordinateSpanMake(0.01 , 0.01)
        let region:MKCoordinateRegion = MKCoordinateRegionMake(coordinates, span)

        var pointAnnotation:MKPointAnnotation = MKPointAnnotation()
        pointAnnotation.coordinate = coordinates
        pointAnnotation.title = locationTitle
        pointAnnotation.subtitle = addressString

        map.addAnnotation(pointAnnotation)
        map.centerCoordinate = coordinates
        map.setRegion(region, animated: true)
        map.selectAnnotation(pointAnnotation, animated: true)
    }
})

关于Swift 2.0 GeoCoder 参数列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30741481/

相关文章:

ios - 如何从公司名称中获取坐标?

ios - 从 Firebase 获取数据为零

ios - 取消完成 block

thread-safety - 无法在线程之间发送 &str,因为它的生存时间不够长

swift - 将 FIRDataSnapshot 转换为自定义类型

php - 如何通过 PHP 使用 Nominatim API 检索经纬度?

ios - 如何绘制一个点(Swift)

ios - 访问 Apple Watch 中的心率传感器 - HealthKit 不返回值

iOS swift : UIWebView has extremely big content

google-maps - 仅通过地理编码获取 IATA 代码