2

我已经像这样设置了 Google GMSAutocomplete:

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
}

并且还实现了代表,UITextFieldDelegateGMSAutocompleteTableDataSourceDelegate它工作得很好。但是,我想将搜索限制在地区、行政区域和国家。例如,我不希望它搜索“地点”。让

我知道有过滤器对象;但是,我如何将它应用到我的使用中?有任何想法吗?

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

4

2 回答 2

2

您可以设置.GMSAutocompleteTableDataSource

于 2016-01-19T22:19:30.777 回答
0

谷歌自动完成 api

func placeAutocomplete() { //Call function from your search editing method
 let filter = GMSAutocompleteFilter()
 filter.type = GMSPlacesAutocompleteTypeFilter.City
  //"YOUR_SEARCH_DATA" Ex. Sydney Oper
 //Uou will get data for  city sydney
 placesClient?.autocompleteQuery("YOUR_SEARCH_DATA", bounds: nil, filter: filter, callback: { (results, error: NSError?) -> Void in
  if let error = error {
    println("Autocomplete error \(error)")
 }
  for result in results! {
  if let result = result as? GMSAutocompletePrediction {
    println("Result \(result.attributedFullText) with placeID \(result.placeID)")
  }
  }
 })
}
于 2016-01-19T06:47:12.927 回答