简体   繁体   中英

How to limit Google Places Autocomplete to not include Address?

How to limit Google Places Autocomplete to not include Address? I currently have my setup

class FilterVC: UIViewController, GMSAutocompleteViewControllerDelegate {

// MARK: SEARCH BAR

@IBAction func searchBarAction(_ sender: Any) {
    let autocompleteController = GMSAutocompleteViewController()
    autocompleteController.delegate = self
    placeAutocomplete(resultsViewController: autocompleteController)
    UINavigationBar.appearance().barTintColor = UIColor.white
    UINavigationBar.appearance().tintColor = UIColor.hiGreyishBrownTwo
    UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes = [NSAttributedStringKey.foregroundColor.rawValue: UIColor.hiGreyishBrownTwo]
    present(autocompleteController, animated: true, completion: nil)
}
func placeAutocomplete(resultsViewController: GMSAutocompleteViewController) {
    var placeClient = GMSPlacesClient()
    let filter = GMSAutocompleteFilter()
    filter.type = .city
    filter.country = "USA"
    resultsViewController.autocompleteFilter = filter

    func viewController(_ viewController: GMSAutocompleteViewController, didAutocompleteWith place: GMSPlace) {
        DataService.instance.place = place
        FillAddress(place: place)
        fillAddressForm()

        print(DataService.instance._address_line1)
         print(DataService.instance._city)
         print(DataService.instance._postalCode)
         print(DataService.instance._state)
         print(DataService.instance._country)
        DataService.instance.addressLabel = place.formattedAddress
        dismiss(animated: true, completion: nil)
}

func wasCancelled(_ viewController: GMSAutocompleteViewController) {
    dismiss(animated: true, completion: nil)
}
func viewController(_ viewController: GMSAutocompleteViewController, didFailAutocompleteWithError error: Error) {
    print("ERROR \(error) Autocomplete")
}

I tried using a GMSAutocompleteFilter to limit my results to only include state city, zip, country. I don't want to display address in the autocomplete controller. When I do this, it only displays Country and city, I can't enter zip code. How would I make that available? I'm not quite sure what I'm missing or what the next step to take is. Any suggestions would be much appreciated.

I was going to take this off since I found a solution, but just in case some one runs into a similar issue, a very easy fix is to set filter type to region.

func placeAutocomplete(resultsViewController: GMSAutocompleteViewController) {
    var placeClient = GMSPlacesClient()
    let filter = GMSAutocompleteFilter()
    filter.country = "USA"
    filter.type = .region

    resultsViewController.autocompleteFilter = filter 

This will limit the search to not include address. When trying .city, that eliminated to much. Well hope this helps someone in the future.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM