Reputation: 99
My current code to populate a tableView with addresses from the Google Places API endpoint is as follows:
However, I want to create a custom client to handle the functionality that autoCompleteQuery, provided by Places, takes care of but manually. I'm assuming this calls for the address JSON to be parsed again, and iterated through, then stored in an array. Please let me know if you have a solution. The commented code works perfectly, I'm trying to achieve the same result manually.
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
}
}
Upvotes: 2
Views: 1786
Reputation: 99
SOLVED: Here's how I ultimately solved the problem of updating UITableView rows with autocomplete when text updated from a UISearchBar.
func searchBar(searchBar: UISearchBar, textDidChange searchText: String){
self.resultsArray.removeAll()
gmsFetcher?.sourceTextHasChanged(searchText)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
//Implement GMSAutoCompleteFetcherDelegate protocol to handle custom string prediction
extension MainMapViewController: GMSAutocompleteFetcherDelegate {
func didAutocompleteWithPredictions(predictions: [GMSAutocompletePrediction]) {
for prediction in predictions {
if let prediction = prediction as GMSAutocompletePrediction!{
self.resultsArray.append(prediction.attributedFullText.string)
}
}
self.searchResultsTable.reloadDataWithArray(self.resultsArray)
print(resultsArray)
}
func didFailAutocompleteWithError(error: NSError) {
resultText?.text = error.localizedDescription
}
}
Upvotes: 2
Reputation: 8092
You can use GMSAutocompleteFetcher
, which wraps the autocompleteQuery method on GMSPlacesClient
. The fetcher throttles requests, returning only results for the most recently entered search text and provides no UI elements.
Steps to implement 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.
Sample code demonstrating the implementation steps in using the fetcher can be found in Use the fetcher.
Upvotes: 3