Kashif
Kashif

Reputation: 4632

Swift: Google Place Picker, Avoiding the Map UI

In my Swift iOS App, I want to take the user to place autocomplete search screen and let them search in context to their current location. Apparently this is not possible with Google Place Autocomplete since there is no way to pass current location context to it.

My second choice is to use Google Place Picker's search screen because when I start Place Picker centred on current location and then tap search, it searches places in context with current location.

My question is, is it possible to take the user directly to Place Picker's search screen and then dismiss the Place Picker after grabbing the picked place information, avoid the main UI of Place Picker?

Upvotes: 1

Views: 4963

Answers (1)

lonesomewhistle
lonesomewhistle

Reputation: 816

It's a little confusing in the docs, but I think what you want is to use the GMSAutocompleteViewController instead of the place picker.

Sample code below, links to the docs here.

import UIKit
import GooglePlaces

class ViewController: UIViewController {

  // Present the Autocomplete view controller when the button is pressed.
  @IBAction func autocompleteClicked(_ sender: UIButton) {
    let autocompleteController = GMSAutocompleteViewController()
    autocompleteController.delegate = self
    present(autocompleteController, animated: true, completion: nil)
  }
}

extension ViewController: GMSAutocompleteViewControllerDelegate {

  // Handle the user's selection.
  func viewController(_ viewController: GMSAutocompleteViewController, didAutocompleteWith place: GMSPlace) {
    print("Place name: \(place.name)")
    print("Place address: \(place.formattedAddress)")
    print("Place attributions: \(place.attributions)")
    dismiss(animated: true, completion: nil)
  }

  func viewController(_ viewController: GMSAutocompleteViewController, didFailAutocompleteWithError error: Error) {
    // TODO: handle the error.
    print("Error: ", error.localizedDescription)
  }

  // User canceled the operation.
  func wasCancelled(_ viewController: GMSAutocompleteViewController) {
    dismiss(animated: true, completion: nil)
  }

  // Turn the network activity indicator on and off again.
  func didRequestAutocompletePredictions(_ viewController: GMSAutocompleteViewController) {
    UIApplication.shared.isNetworkActivityIndicatorVisible = true
  }

  func didUpdateAutocompletePredictions(_ viewController: GMSAutocompleteViewController) {
    UIApplication.shared.isNetworkActivityIndicatorVisible = false
  }

}

Upvotes: 1

Related Questions