本文介绍了以相同的方式在GMSAutocomplete中使用textfield而不是搜索栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用google Place Autocomplete API,我需要添加UITextField而不是具有相同功能的UISearchBar。这里是UISearchBar的工作代码,我从。我会自己添加textfield和tableview,如果有人只是帮助我从任何搜索关键字获取地址数组。例如从字符串(关键字)到数组(预测位置)。

I am using google Place Autocomplete API, i need to add UITextField instead of UISearchBar with the same functionality. here is the working code with UISearchBar which i get from https://developers.google.com/places/ios-api/autocomplete. I will add textfield and tableview myself, if someone just help me to get array of addresses from any keyword of searching. like from string (of keyword) to array(predicted places).

import UIKit
import GoogleMaps

class ViewController: UIViewController {

var resultsViewController: GMSAutocompleteResultsViewController?
var searchController: UISearchController?
var resultView: UITextView?

override func viewDidLoad() {
    super.viewDidLoad()

    resultsViewController = GMSAutocompleteResultsViewController()
    resultsViewController?.delegate = self

    searchController = UISearchController(searchResultsController: resultsViewController)
    searchController?.searchResultsUpdater = resultsViewController

    let subView = UIView(frame: CGRectMake(0, 65.0, 350.0, 45.0))

    subView.addSubview((searchController?.searchBar)!)
    self.view.addSubview(subView)
    searchController?.searchBar.sizeToFit()
    searchController?.hidesNavigationBarDuringPresentation = false
    self.definesPresentationContext = true
}
}

extension ViewController: GMSAutocompleteResultsViewControllerDelegate {
func resultsController(resultsController: GMSAutocompleteResultsViewController,
    didAutocompleteWithPlace place: GMSPlace) {
        searchController?.active = false
        print("Place name: ", place.name)
        print("Place address: ", place.formattedAddress!)
}

func resultsController(resultsController: GMSAutocompleteResultsViewController,
    didFailAutocompleteWithError error: NSError){
        print("Error: ", error.description)
}

func didRequestAutocompletePredictionsForResultsController(resultsController: GMSAutocompleteResultsViewController) {
    UIApplication.sharedApplication().networkActivityIndicatorVisible = true
}

func didUpdateAutocompletePredictionsForResultsController(resultsController: GMSAutocompleteResultsViewController) {
    UIApplication.sharedApplication().networkActivityIndicatorVisible = false
}
}


推荐答案

文本字段和表格视图。然后钩住它。整合您的GPA(Google Places API)并最终运行它。

At first take a text field and a table view. Then hook it. Integrate your GPA(Google Places API) and finally run it.

import UIKit
import GooglePlaces

class YourViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {


    @IBOutlet weak var PlaceTextField: UITextField!

    @IBOutlet weak var tableView: UITableView!

    var tableData=[String]()

    var fetcher: GMSAutocompleteFetcher?

    override func viewDidLoad() {
        super.viewDidLoad()

        self.view.backgroundColor = UIColor.whiteColor()
        self.edgesForExtendedLayout = .None

        // 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

        PlaceTextField?.addTarget(self, action: "textFieldDidChange:",forControlEvents: .EditingChanged)


        tableView.delegate = self
        tableView.dataSource = self

        self.tableView.reloadData()

    }

    func textFieldDidChange(textField: UITextField) {
        fetcher?.sourceTextHasChanged(PlaceTextField.text!)
    }

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

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


    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        var section = indexPath.section

        var row = indexPath.row

        let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier:"addCategoryCell")

        cell.selectionStyle =  UITableViewCellSelectionStyle.None
        cell.backgroundColor = UIColor.clearColor()
        cell.contentView.backgroundColor = UIColor.clearColor()
        cell.textLabel?.textAlignment = NSTextAlignment.Left
        cell.textLabel?.textColor = UIColor.blackColor()
        cell.textLabel?.font = UIFont.systemFontOfSize(14.0)

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

        return cell
    }


}

extension YourViewController: GMSAutocompleteFetcherDelegate {

    func didAutocompleteWithPredictions(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: NSError) {
        //resultText?.text = error.localizedDescription
        print(error.localizedDescription)
    }
}

这篇关于以相同的方式在GMSAutocomplete中使用textfield而不是搜索栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 08:25