[英]Get searched places by GoogleApi to fill in tableview
我試過 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)*")
}
您應該使用數組作為結果而不是 let resultsStr = NSMutableString()
並使用數據源作為數組填充表視圖
您需要使用GMSAutocompleteFilter
過濾器進行調整以獲得所需的結果。 當前,您正在使用過濾器類型過濾結果: .establishment
,它過濾/限制搜索結果。 您可以考慮使用.noFilter
或任何其他過濾器。
let filter = GMSAutocompleteFilter()
filter.type = .noFilter
引用enum GMSPlacesAutocompleteTypeFilter
,來源: GMSAutocompleteFilter.h
public enum GMSPlacesAutocompleteTypeFilter : Int {
/**
* All results.
*/
case noFilter
/**
* Geeocoding results, as opposed to business results.
*/
case geocode
/**
* Geocoding results with a precise address.
*/
case address
/**
* Business results.
*/
case establishment
/**
* Results that match the following types:
* "locality",
* "sublocality"
* "postal_code",
* "country",
* "administrative_area_level_1",
* "administrative_area_level_2"
*/
case region
/**
* Results that match the following types:
* "locality",
* "administrative_area_level_3"
*/
case city
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.