如何在Swift 3中转换Place Picker Documentation代码?

时间:2016-10-28 05:35:30

标签: ios google-maps swift3

放置选择器

@IBAction func pickPlace(sender: UIBarButtonItem) {
  let center = CLLocationCoordinate2DMake(51.5108396, -0.0922251)
  let northEast = CLLocationCoordinate2DMake(center.latitude + 0.001, center.longitude + 0.001)
  let southWest = CLLocationCoordinate2DMake(center.latitude - 0.001, center.longitude - 0.001)
  let viewport = GMSCoordinateBounds(coordinate: northEast, coordinate: southWest)
  let config = GMSPlacePickerConfig(viewport: viewport)
  placePicker = GMSPlacePicker(config: config)

  placePicker?.pickPlaceWithCallback({ (place: GMSPlace?, error: NSError?) -> Void in
    if let error = error {
      print("Pick Place error: \(error.localizedDescription)")
      return
    }

    if let place = place {
      print("Place name \(place.name)")
      print("Place address \(place.formattedAddress)")
      print("Place attributions \(place.attributions)")
    } else {
      print("No place selected")
    }
  })
}

Place Picker in Swift 2 in Doucmentation of Google Map

在Swift 3中进行转换

   placePicker?.pickPlace(callback: { (place: GMSPlace!, error: NSError!) -> Void in
            if let error = error {
                print("Pick Place error: \(error.localizedDescription)")
                return
            }

            if let place = place {

}
        } as! GMSPlaceResultCallback)

并提供错误指令访问错误

1 个答案:

答案 0 :(得分:1)

试试这个。

placePicker.pickPlace(callback: { (place, err) -> Void in {

if let error = error {
print("lookup place id query error: \(error.localizedDescription)")
return
}

if let place = place {
print("Place name \(place.name)")
} else {
print("No place details for \(placeID)")
}
})