ios - 获取GoogleApi搜索到的地方填入tableview

标签 ios swift google-places gmsplace

我已经尝试过 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
}

搜索“Mex”的结果

> 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)*")
}

最佳答案

你应该为结果使用数组而不是让 resultsStr = NSMutableString()

并用数据源作为数组填充 TableView

关于ios - 获取GoogleApi搜索到的地方填入tableview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46464406/

相关文章:

Swift.Streaming 来自 url 的音频,错误代码为 -999 "cancelled"

google-places-api - 在 Google Places Autocomplete 上查找地址时获取城市 place-id

ios - 有没有办法在方法完成后运行代码?

swift - 在 SwiftUI/AVPlayer 中与 NotificationCenter/Combine 作斗争

ios - 专为 Iphone 6 设计的调整大小应用程序可在 iphone 5s 中正确缩放

swift - 带小数的 UIStepper

javascript - Google Maps API,加载 Onion.js 时挂起(昨天开始)

android - Google PlacePicker 在启动后立即关闭

ios - 将 0-1 值以指数方式映射到快门速度列表

ios - 无法从 iOS OAuth1.0、OAConsumer 客户端在 tumblr 上发帖