Search code examples
iosswiftgoogle-places-apigmsautocomplete

Google Places API, GMSAutocomplete search only for City, Country iOS Swift


I have setup the Google GMSAutocomplete like so:

private func setupAutocompleteWithTextFieldController() {
    let kTextFieldTop: CGFloat = searchField.frame.origin.y
    let kTextFieldHeight: CGFloat = 44.0
    let kContentRectTop = kTextFieldTop + kTextFieldHeight

    contentRect = CGRectMake(0, kContentRectTop, self.view.bounds.size.width, self.view.bounds.size.height - kTextFieldHeight)

    searchField.autocorrectionType = .No
    searchField.returnKeyType = .Done
    searchField.clearButtonMode = .WhileEditing
    searchField.addTarget(self, action: "searchFieldTextFieldEditingChanged:", forControlEvents: UIControlEvents.EditingChanged)
    searchField.delegate = self

    tableDataSource = GMSAutocompleteTableDataSource()
    tableDataSource.delegate = self

    resultsController = UITableViewController(style: UITableViewStyle.Plain)
    resultsController.tableView.delegate = tableDataSource
    resultsController.tableView.dataSource = tableDataSource
}

and also implemented the delegates, UITextFieldDelegate, GMSAutocompleteTableDataSourceDelegate and it's working great. However, I would like to limit the search to locality, administrativeArea and country. e.g. I don't want it to search for 'Places'. let

I know there is the filter object; however, how do I apply it to my use? Any ideas?

filter = GMSAutocompleteFilter(), filter.type = GMSPlacesAutocompleteTypeFilter.City


Solution

  • You can set the autocompleteFilter field of the GMSAutocompleteTableDataSource.