gpt4 book ai didi

swift - Google Places Autocomplete API 未在我的 Tableview 中填充确切的地址

转载 作者:行者123 更新时间:2023-11-30 10:31:41 25 4
gpt4 key购买 nike

我正在使用 google Place Autocomplete API,我有 UITextField 而不是 UISearchBar 具有相同的功能;我正在将搜索估计填充到 tableView 中。然而,结果并没有显示确切的地址;相反,它只显示地点名称。我怎样才能使表格 View 中的结果是确切的地址而不是地名?

这是我的代码:

class DeliveryAddressVC: UIViewController, UITableViewDelegate, UITableViewDataSource {

@IBOutlet weak var streetTextField: UITextField!
@IBAction func searchTextClicked(_ sender: Any) {}
@IBOutlet weak var tableView: UITableView!


var tableData=[String]()
var fetcher: GMSAutocompleteFetcher?

override func viewDidLoad() {
super.viewDidLoad()

if streetTextField.text == "" {
tableView.isHidden = true
}

self.edgesForExtendedLayout = []

// Set bounds to inner-west Sydney Australia.
let neBoundsCorner = CLLocationCoordinate2D(latitude: -33.843366,
longitude: 151.134002)
let swBoundsCorner = CLLocationCoordinate2D(latitude: -33.875725,
longitude: 151.200349)
let bounds = GMSCoordinateBounds(coordinate: neBoundsCorner,
coordinate: swBoundsCorner)

// Set up the autocomplete filter.
let filter = GMSAutocompleteFilter()
filter.type = .establishment

// Create the fetcher.
fetcher = GMSAutocompleteFetcher(bounds: bounds, filter: filter)
fetcher?.delegate = self as GMSAutocompleteFetcherDelegate

streetTextField.addTarget(self, action: #selector(DeliveryAddressVC.textFieldDidChanged(_:)), for: UIControl.Event.editingChanged)

tableView.delegate = self
tableView.dataSource = self

tableView.reloadData()
}

// MARK: -UITextField Action

@objc func textFieldDidChanged(_ textField:UITextField ){

if streetTextField.text == "" {
tableView.isHidden = true
}else {
tableView.isHidden = false
}
fetcher?.sourceTextHasChanged(streetTextField.text!)
}

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tableData.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

var section = indexPath.section

var row = indexPath.row

let cell: UITableViewCell = UITableViewCell(style: UITableViewCell.CellStyle.default, reuseIdentifier:"addCategoryCell")

cell.selectionStyle = UITableViewCell.SelectionStyle.none
cell.backgroundColor = UIColor.clear
cell.contentView.backgroundColor = UIColor.clear
cell.textLabel?.textAlignment = NSTextAlignment.left
cell.textLabel?.textColor = UIColor.black
cell.textLabel?.font = UIFont.systemFont(ofSize: 14.0)

cell.textLabel?.text = tableData[indexPath.row]
return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.isHidden = true
}



}
extension DeliveryAddressVC: GMSAutocompleteFetcherDelegate {
func didAutocomplete(with predictions: [GMSAutocompletePrediction]) {
tableData.removeAll()

for prediction in predictions {

tableData.append(prediction.attributedPrimaryText.string)

//print("\n",prediction.attributedFullText.string)
//print("\n",prediction.attributedPrimaryText.string)
//print("\n********")
}

tableView.reloadData()
}

func didFailAutocompleteWithError(_ error: Error) {
print(error.localizedDescription)
}


}

最佳答案

找到了解决方案;我必须更改代码中的两行:

改变了这一点:filter.type = .assessment

收件人:filter.type = .address

这个:tableData.append(prediction.attributedPrimaryText.string)

至:tableData.append(prediction.attributedFullText.string)

关于swift - Google Places Autocomplete API 未在我的 Tableview 中填充确切的地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59075140/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com