ios – Google自动填充教程为swift提供了api

ios – Google自动填充教程为swift提供了api,第1张

概述我想有一个自动完成文本字段,为我自动填充位置,如 android的那个: https://developers.google.com/places/training/autocomplete-android 有谁知道我在哪里可以找到这个或一个例子的教程? 谢谢! 脚步 : >在swift项目中添加Alamofire CocoaPods. >在Google API控制台上查找您的Google地方AP 我想有一个自动完成文本字段,为我自动填充位置,如 android的那个:

https://developers.google.com/places/training/autocomplete-android

有谁知道我在哪里可以找到这个或一个例子的教程?

谢谢!

解决方法 脚步 :

>在swift项目中添加Alamofire CocoaPods.
>在Google Api控制台上查找您的Google地方API密钥.
>添加以下代码

VIEwController.swift

import UIKitclass 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 UIKitimport Alamofireenum 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: - GooglePlacesautocompleteclass 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: - GooglePlaceSearchdisplayControllerclass 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: - GooglePlacesautocompleteContainerclass 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所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1114939.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存