import UIKit
import GooglePlaces
class LocationViewController: UIViewController {
var resultsViewController: GMSAutocompleteResultsViewController?
var searchController: UISearchController?
override func viewDidLoad() {
super.viewDidLoad()
self.title = "Select Addres".localised
resultsViewController = GMSAutocompleteResultsViewController()
resultsViewController?.delegate = self
searchController = UISearchController(searchResultsController: resultsViewController)
searchController?.searchResultsUpdater = resultsViewController
let subView = UIView(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: 45.0))
subView.addSubview((searchController?.searchBar)!)
view.addSubview(subView)
definesPresentationContext = true
// Do any additional setup after loading the view.
}
@IBAction func cancelAct(_ sender: Any) {
self.dismiss(animated: true, completion: nil)
}
}
extension LocationViewController: GMSAutocompleteResultsViewControllerDelegate {
func resultsController(_ resultsController: GMSAutocompleteResultsViewController,
didAutocompleteWith place: GMSPlace) {
searchController?.isActive = false
// Do something with the selected place.
print("Place Address: \(place.formattedAddress)")
searchController?.searchBar.text = place.formattedAddress
self.dismiss(animated: true, completion: nil)
}
func resultsController(_ resultsController: GMSAutocompleteResultsViewController,
didFailAutocompleteWithError error: Error){
// TODO: handle the error.
print("Error: ", error.localizedDescription)
}
// Turn the network activity indicator on and off again.
func didRequestAutocompletePredictions(forResultsController resultsController: GMSAutocompleteResultsViewController) {
UIApplication.shared.isNetworkActivityIndicatorVisible = true
}
func didUpdateAutocompletePredictions(forResultsController resultsController: GMSAutocompleteResultsViewController) {
UIApplication.shared.isNetworkActivityIndicatorVisible = false
}
}
Be the first to comment.