AutocompletePushViewController.swift 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 Autocomplete view controller pushed on the navigation stack. Please refer to
  16. /// https://developers.google.com/places/ios-sdk/autocomplete
  17. class AutocompletePushViewController: AutocompleteBaseViewController {
  18. private let margin: CGFloat = 80
  19. private let buttonHeight: CGFloat = 50
  20. private lazy var showWidgetButton: UIButton = {
  21. let button = UIButton()
  22. button.setTitle(
  23. NSLocalizedString(
  24. "Demo.Content.Autocomplete.ShowWidgetButton",
  25. comment: "Button title for 'show autocomplete widget'"), for: .normal)
  26. button.setTitleColor(.darkGray, for: .normal)
  27. button.translatesAutoresizingMaskIntoConstraints = false
  28. button.addTarget(self, action: #selector(showAutocompleteWidget), for: .touchUpInside)
  29. return button
  30. }()
  31. override func viewDidLoad() {
  32. super.viewDidLoad()
  33. view.backgroundColor = .white
  34. automaticallyAdjustsScrollViewInsets = true
  35. view.addSubview(showWidgetButton)
  36. NSLayoutConstraint.activate([
  37. showWidgetButton.leadingAnchor.constraint(equalTo: view.leadingAnchor),
  38. showWidgetButton.trailingAnchor.constraint(equalTo: view.trailingAnchor),
  39. showWidgetButton.topAnchor.constraint(equalTo: view.topAnchor, constant: margin),
  40. showWidgetButton.heightAnchor.constraint(equalToConstant: buttonHeight),
  41. ])
  42. }
  43. @objc func showAutocompleteWidget() {
  44. showWidgetButton.isHidden = true
  45. let autocompleteViewController = GMSAutocompleteViewController()
  46. autocompleteViewController.delegate = self
  47. if let config = autocompleteConfiguration {
  48. autocompleteViewController.autocompleteFilter = config.autocompleteFilter
  49. autocompleteViewController.placeFields = config.placeFields
  50. }
  51. navigationController?.pushViewController(autocompleteViewController, animated: true)
  52. }
  53. }
  54. extension AutocompletePushViewController: GMSAutocompleteViewControllerDelegate {
  55. func viewController(
  56. _ viewController: GMSAutocompleteViewController, didAutocompleteWith place: GMSPlace
  57. ) {
  58. navigationController?.popToViewController(self, animated: true)
  59. super.autocompleteDidSelectPlace(place)
  60. }
  61. func viewController(
  62. _ viewController: GMSAutocompleteViewController, didFailAutocompleteWithError error: Error
  63. ) {
  64. navigationController?.popToViewController(self, animated: true)
  65. super.autocompleteDidFail(error)
  66. }
  67. func wasCancelled(_ viewController: GMSAutocompleteViewController) {
  68. navigationController?.popToViewController(self, animated: true)
  69. super.autocompleteDidCancel()
  70. }
  71. }