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
}
}
Integrate your GPA(Google Places API). POD files are :
pod 'GooglePlaces'
pod 'GooglePlacePicker'
pod 'GoogleMaps'
At first, provide your KEY to AppDelegate.swift
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
GMSPlacesClient.provideAPIKey("Your KEY")
return true
}
At first, take a text field and a table view. Then add these line to your ViewController.swift file and hook the textField and tableView and finally run it.
import UIKit
import GooglePlaces
class ViewController: 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.lightGray
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
PlaceTextField.addTarget(self, action: #selector(ViewController.textFieldDidChanged(_:)), for: UIControlEvents.editingChanged)
tableView.delegate = self
tableView.dataSource = self
tableView.reloadData()
}
// MARK: -UITextField Action
@objc func textFieldDidChanged(_ textField:UITextField ){
print(PlaceTextField.text!)
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, cellForRowAt indexPath: IndexPath) -> 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.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
}
}
extension ViewController: 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)
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With