适用于iOS的Google Places API不允许我取消或选择

时间:2017-12-04 09:41:32

标签: ios swift google-places-api

我已经关注了Google网站上的Places API教程,我已经仔细检查过启用了适用于iOS的Places API以及我的API密钥是否在我的AppDelegate中。我已经在我的班级中实现了GMSPlacePickerViewControllerDelegate。地点选择器将打开,我可以查看地点,我可以点击它们,我可以搜索。我无法离开这个屏幕,我无法取消,我实际上无法选择任何东西或返回一个位置。这在物理设备和模拟器上是相同的。为什么它不起作用,我怎样才能使它发挥作用?

@IBAction func selectLocationPressed(_ sender: Any) {
    let config = GMSPlacePickerConfig(viewport: nil)
    let placePicker = GMSPlacePickerViewController(config: config)
    present(placePicker, animated: true, completion: nil)
}

// To receive the results from the place picker 'self' will need to conform to
// GMSPlacePickerViewControllerDelegate and implement this code.
func placePicker(_ viewController: GMSPlacePickerViewController, 
didPick place: GMSPlace) {
// Dismiss the place picker, as it cannot dismiss itself.
viewController.dismiss(animated: true, completion: nil)

print("Place name \(place.name)")
print("Place address \(place.formattedAddress)")
print("Place attributions \(place.attributions)")
}

func placePickerDidCancel(_ viewController: 
GMSPlacePickerViewController) {
// Dismiss the place picker, as it cannot dismiss itself.
viewController.dismiss(animated: true, completion: nil)

print("No place selected")
}

1 个答案:

答案 0 :(得分:5)

在您的班级中实施GMSPlacePickerViewControllerDelegate协议并设置placePicker.delegate = self