我当前使用来自 Google Places API 端点的地址填充 tableView 的代码如下:
但是,我想创建一个自定义客户端来处理由 Places 提供的 autoCompleteQuery 手动处理的功能。我假设这需要再次解析地址 JSON,并迭代,然后存储在数组中。如果您有解决方案,请告诉我。注释的代码运行完美,我正在尝试手动实现相同的结果。
func searchBar(searchBar: UISearchBar, textDidChange searchText: String){
// let mapsClient = GMSPlacesClient()
// mapsClient.autocompleteQuery(searchText, bounds: nil, filter: nil {(results, error: NSError?) in
//
// self.resultsArray.removeAll()
//
// if results == nil{
// return
// }
//
// for result in results! {
//
// if let result = result as GMSAutocompletePrediction! {
// self.resultsArray.append(result.attributedFullText.string)
// }
// }
//
// self.searchResultsClient.reloadDataWithArray(self.resultsArray)
// }
gmsFetcher?.sourceTextHasChanged(searchText)
self.searchResultsClient.reloadDataWithArray(self.resultsArray)
print(resultsArray)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
extension MainMapViewController: GMSAutocompleteFetcherDelegate {
func didAutocompleteWithPredictions(predictions: [GMSAutocompletePrediction]) {
self.resultsArray.count + 1
let resultsStr = NSMutableString()
for prediction in predictions {
resultsStr.appendFormat("%@\n", prediction.attributedPrimaryText)
}
resultText?.text = resultsStr as String
self.resultsArray.append(resultsStr as String)
self.searchResultsClient.reloadDataWithArray(self.resultsArray)
}
func didFailAutocompleteWithError(error: NSError) {
resultText?.text = error.localizedDescription
}
}
最佳答案
您可以使用 GMSAutocompleteFetcher
,它将 autocompleteQuery 方法包装在 GMSPlacesClient
上。提取器会限制请求,仅返回最近输入的搜索文本的结果,并且不提供任何 UI 元素。
实现 GMSAutocompleteFetcher 的步骤:
- Implement the
GMSAutocompleteFetcherDelegate
protocol.- Create a
GMSAutocompleteFetcher
object.- Call
sourceTextHasChanged
on the fetcher as the user types.- Handle predictions and errors using the
didAutcompleteWithPredictions
anddidFailAutocompleteWithError
protocol methods.
演示使用 fetcher 的实现步骤的示例代码可以在 Use the fetcher 中找到。 .
关于ios - 尝试复制 GMSPlacesClient : autoCompleteQuery function to populate a UITableView manually,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36640502/