ios - 尝试复制 GMSPlacesClient : autoCompleteQuery function to populate a UITableView manually

标签 ios swift google-maps autocomplete google-places-api

我当前使用来自 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 的步骤:

  1. Implement the GMSAutocompleteFetcherDelegate protocol.
  2. Create a GMSAutocompleteFetcher object.
  3. Call sourceTextHasChanged on the fetcher as the user types.
  4. Handle predictions and errors using the didAutcompleteWithPredictions and didFailAutocompleteWithError protocol methods.

演示使用 fetcher 的实现步骤的示例代码可以在 Use the fetcher 中找到。 .

关于ios - 尝试复制 GMSPlacesClient : autoCompleteQuery function to populate a UITableView manually,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36640502/

相关文章:

ios - 如何创建在安装应用程序时添加的初始 Realm 对象

ios - UIImageView 动画崩溃(sigabrt)

ios - 对 React Native App 的离线支持

Swift:将对象实例名称转换为字符串以用作键

ios - AVAudioEngine & AVAudioPlayerNode didFinish 方法类似于 AVAudioPlayer

google-maps - 在没有 iframe 的网站上嵌入 map

javascript - 我的谷歌放置了 kml url javascript

ios - 缩放或移动时谷歌地图滞后

ios - 通过 Google map 或 Apple map (iOS) 使用自定义路线

ios - Swift 错误说“实例成员 * 不能在类型上使用”