我尝试过 GMSPlacesClient,但无法以正确的格式获得结果以将它们填充到 tableView 中。
之后我使用了 Fetcher,但我没有帮助我。
extension SearchViewController: GMSAutocompleteFetcherDelegate {
func didAutocomplete(with predictions: [GMSAutocompletePrediction]) {
let resultsStr = NSMutableString()
for prediction in predictions {
resultsStr.appendFormat("%@\n", prediction.attributedPrimaryText)
}
print(resultsStr as String)
}
func didFailAutocompleteWithError(_ error: Error) {
print(error.localizedDescription)
}
}
我不知道我是否不明白它的用法。有人可能会描述我如何使用 Google Places Api 在 textBox 中搜索地址,然后在 tableView 中显示结果。
我的问题是只得到搜索结果?
获取零件代码:
var fetcher: GMSAutocompleteFetcher?
func placeAutocomplete(add:String) {
fetcher?.sourceTextHasChanged(add)
}
override func viewDidLoad() {
super.viewDidLoad()
tblView.delegate = self
tblView.dataSource = self
let filter = GMSAutocompleteFilter()
filter.type = .establishment
// Create the fetcher.
fetcher = GMSAutocompleteFetcher(bounds: nil, filter: filter)
fetcher?.delegate = self
}
搜索“墨西哥”的结果
> Me{
GMSAutocompleteMatch = "<GMSAutocompleteMatchFragment: 0x60800043db60>";
}rcado de La Boqueria, La Rambla, Barcelona, Spain{
}*
Me{
GMSAutocompleteMatch = "<GMSAutocompleteMatchFragment:
0x60800043db60>";
}rcado San Anton, Calle de Augusto Figueroa, Madrid, Spain{
}*
Me{
GMSAutocompleteMatch = "<GMSAutocompleteMatchFragment: 0x60800043db60>";
}nlyn Mall, Atterbury Road, Pretoria, South Africa{
}*
Me{
GMSAutocompleteMatch = "<GMSAutocompleteMatchFragment:
0x60800043db60>";
}ga Mall, Bulevardul Pierre de Coubertin, Bucharest, Romania{
}*
Me{
GMSAutocompleteMatch = "<GMSAutocompleteMatchFragment:
0x60800043db60>";
}rcado de San Ildefonso, Calle de Fuencarral, Madrid, Spain{
}*
打印代码:
for q in predictions1
{
print ( "\(q.attributedFullText)*")
}