AutocompleteModalViewController.swift 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // Copyright 2020 Google LLC. All rights reserved.
  2. //
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
  5. // file except in compliance with the License. You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software distributed under
  10. // the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
  11. // ANY KIND, either express or implied. See the License for the specific language governing
  12. // permissions and limitations under the License.
  13. import GooglePlaces
  14. import UIKit
  15. /// Demo showing a modally presented Autocomplete view controller. Please refer to
  16. /// https://developers.google.com/places/ios-sdk/autocomplete
  17. class AutocompleteModalViewController: AutocompleteBaseViewController {
  18. override func viewDidLoad() {
  19. super.viewDidLoad()
  20. let autocompleteViewController = GMSAutocompleteViewController()
  21. autocompleteViewController.delegate = self
  22. if let config = autocompleteConfiguration {
  23. autocompleteViewController.autocompleteFilter = config.autocompleteFilter
  24. autocompleteViewController.placeFields = config.placeFields
  25. }
  26. navigationController?.present(autocompleteViewController, animated: true)
  27. }
  28. }
  29. extension AutocompleteModalViewController: GMSAutocompleteViewControllerDelegate {
  30. func viewController(
  31. _ viewController: GMSAutocompleteViewController,
  32. didAutocompleteWith place: GMSPlace
  33. ) {
  34. navigationController?.dismiss(animated: true)
  35. super.autocompleteDidSelectPlace(place)
  36. }
  37. func viewController(
  38. _ viewController: GMSAutocompleteViewController,
  39. didFailAutocompleteWithError error: Error
  40. ) {
  41. navigationController?.dismiss(animated: true)
  42. super.autocompleteDidFail(error)
  43. }
  44. func wasCancelled(_ viewController: GMSAutocompleteViewController) {
  45. navigationController?.dismiss(animated: true)
  46. super.autocompleteDidCancel()
  47. }
  48. func didRequestAutocompletePredictions(_ viewController: GMSAutocompleteViewController) {
  49. print("Request autocomplete predictions.")
  50. }
  51. func didUpdateAutocompletePredictions(_ viewController: GMSAutocompleteViewController) {
  52. print("Updated autocomplete predictions.")
  53. }
  54. }