大佬教程收集整理的这篇文章主要介绍了ios – Google自动填充教程为swift提供了api,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
https://developers.google.com/places/training/autocomplete-android
有谁知道我在哪里可以找到这个或一个例子的教程?
谢谢!
>在swift项目中添加Alamofire CocoaPods.
>在Google Api控制台上查找您的Google地方API密钥.
>添加以下代码
ViewController.swift
import UIKit class ViewController: UIViewController { override func viewDidAppear(animated: Bool) { super.viewDidAppear(animated) let gpaViewController = GooglePlacesAutocomplete( apiKey: "YOUR GOOGLE PLACE API KEY",plaCEType: .Address ) gpaViewController.placeDelegate = self presentViewController(gpaViewController,animated: true,completion: nil) } } extension ViewController: GooglePlacesAutocompleteDelegate { func placeSELEcted(place: PlacE) { println(place.description) } func placeViewClosed() { dismissviewControllerAnimated(true,completion: nil) } }
GooglePlacesAutocomplete.swift
import UIKit import Alamofire enum PlaCEType: Printable { case All case Geocode case Address case Establishment case Regions case Cities var description : String { switch self { case .All: return "" case .Geocode: return "geocode" case .Address: return "address" case .Establishment: return "establishment" case .Regions: return "regions" case .Cities: return "cities" } } } struct Place { let id: String let description: String } protocol GooglePlacesAutocompleteDelegate { func placeSELEcted(place: PlacE) func placeViewClosed() } // MARK: - GooglePlacesAutocomplete class GooglePlacesAutocomplete: UINavigationController { var gpaViewController: GooglePlacesAutocompleteContainer? var placeDelegate: GooglePlacesAutocompleteDelegate? { get { return gpaViewController?.delegate } set { gpaViewController?.delegate = newValue } } convenience init(apiKey: String,plaCEType: PlaCEType = .All) { let gpaViewController = GooglePlacesAutocompleteContainer( apiKey: apiKey,plaCEType: plaCEType ) self.init(rootViewController: gpaViewController) self.gpaViewController = gpaViewController let closeButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Stop,target: self,action: "close") gpaViewController.navigationItem.leftBarButtonItem = closeButton gpaViewController.navigationItem.title = "Enter Address" } func close() { placeDelegate?.placeViewClosed() } } // MARK: - GooglePlaceSearchDisplayController class GooglePlaceSearchDisplayController: UISearchDisplayController { override func setActive(visible: Bool,animated: Bool) { if active == visible { return } searchContentsController.navigationController?.navigationBarHidden = true super.setActive(visible,animated: animated) searchContentsController.navigationController?.navigationBarHidden = false if visible { searchBar.becomeFirstResponder() } else { searchBar.resignFirstResponder() } } } // MARK: - GooglePlacesAutocompleteContainer class GooglePlacesAutocompleteContainer: UIViewController { var delegate: GooglePlacesAutocompleteDelegate? var apiKey: String? var places = [Place]() var plaCEType: PlaCEType = .All convenience init(apiKey: String,plaCEType: PlaCEType = .All) { self.init(nibName: "GooglePlacesAutocomplete",bundle: nil) self.apiKey = apiKey self.plaCEType = plaCEType } override func viewDidLoad() { super.viewDidLoad() let tv: UITableView? = searchDisplayController?.searchResultsTableView tv?.registerClass(UITableViewCell.self,forCellReusEIDentifier: "Cell") } } // MARK: - GooglePlacesAutocompleteContainer (UITableViewDatasource / UITableViewDelegatE) extension GooglePlacesAutocompleteContainer: UITableViewDatasource,UITableViewDelegate { func tableView(tableView: UITableView,numberOfRowsInSection section: int) -> Int { return places.count } func tableView(tableView: UITableView,cellForRowATindexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = self.searchDisplayController?.searchResultsTableView?.dequeueReusableCellWithIdentifier("Cell",forIndexPath: indexPath) as UITableViewCell // Get the corresponding cAndy from our candies array let place = self.places[indexPath.row] // Configure the cell cell.textLabel.text = place.description cell.accessoryType = UITableViewCellAccessoryType.DisclosureInDicator return cell } func tableView(tableView: UITableView,didSELEctRowATindexPath indexPath: NSIndexPath) { delegate?.placeSELEcted(self.places[indexPath.row]) } } // MARK: - GooglePlacesAutocompleteContainer (UISearchDisplayDelegatE) extension GooglePlacesAutocompleteContainer: UISearchDisplayDelegate { func searchDisplayController(controller: UISearchDisplayController,shouldReloadTableForSearchString searchString: String!) -> Bool { getPlaces(searchString) return false } private func getPlaces(searchString: String) { Alamofire.request(.GET,"https://maps.googleapis.com/maps/api/place/autocomplete/json",parameters: [ "input": searchString,"type": "(\(plaCEType.description))","key": apiKey ?? "" ]).responseJSON { request,response,json,error in if let response = json as? NSDictionary { if let preDictions = response["preDictions"] as? Array<AnyObject> { self.places = preDictions.map { (preDiction: AnyObject) -> Place in return Place( id: preDiction["id"] as String,description: preDiction["description"] as String ) } } } self.searchDisplayController?.searchResultsTableView?.reloadData() } } }
GooglePlacesAutocomplete.xib
希望这会有助于他人.
以上是大佬教程为你收集整理的ios – Google自动填充教程为swift提供了api全部内容,希望文章能够帮你解决ios – Google自动填充教程为swift提供了api所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。