Google自动填充教程为swift添加了api

Lau*_*yts 7 xcode google-maps objective-c ios swift

我想有一个自动完成文本字段,为我自动填充位置,如android的那个:

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

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

谢谢!

Ash*_*kad 9

脚步 :

  1. 在你的快速项目中添加Alamofire CocoaPods.
  2. 在Google API控制台上查找您的Google地方API密钥.
  3. 添加以下代码

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)
  }
}
Run Code Online (Sandbox Code Playgroud)

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()
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

GooglePlacesAutocomplete.xib

GooglePlacesAutocomplete.xib

希望这会有助于他人.

  • 可打印的内容 (2认同)

Ani*_*pta 5

以下是 Google 自动完成地点 API 的完整更新代码。Xcode 10.0 和 Swift 4.2

点击此链接获取Google API KEY

获取 API KEY 后安装 Cocoa Pods:

source 'https://github.com/CocoaPods/Specs.git'
target 'YOUR_APPLICATION_TARGET_NAME_HERE' do
  pod 'GooglePlaces'
  pod 'GooglePlacePicker'
  pod 'GoogleMaps'
end
Run Code Online (Sandbox Code Playgroud)

应用程序委托文件:

    import UIKit
    import GooglePlaces

    let GOOGLE_API_KEY = "AIzaSyCuZkL7bh_hIDggnJob-b0cDueWlvRgpck"

    @UIApplicationMain
    class AppDelegate: UIResponder, UIApplicationDelegate {

        var window: UIWindow?


        func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
            // Override point for customization after application launch.
            GMSPlacesClient.provideAPIKey(GOOGLE_API_KEY)

            return true
        }

        func applicationWillResignActive(_ application: UIApplication) {
            // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
            // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
        }

        func applicationDidEnterBackground(_ application: UIApplication) {
            // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
            // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
        }

        func applicationWillEnterForeground(_ application: UIApplication) {
            // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
        }

        func applicationDidBecomeActive(_ application: UIApplication) {
            // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
        }

        func applicationWillTerminate(_ application: UIApplication) {
            // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
        }


    }
Run Code Online (Sandbox Code Playgroud)

视图控制器文件:

    import UIKit
    import GooglePlaces


    class ViewController: UIViewController ,CLLocationManagerDelegate{

        var placesClient: GMSPlacesClient!

        // Add a pair of UILabels in Interface Builder, and connect the outlets to these variables.
        @IBOutlet var nameLabel: UILabel!
        @IBOutlet var addressLabel: UILabel!
        let locationManager = CLLocationManager()



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


        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view, typically from a nib.
            locationManager.delegate = self
            if CLLocationManager.authorizationStatus() == .notDetermined
            {
                locationManager.requestAlwaysAuthorization()
            }
            placesClient = GMSPlacesClient.shared()

     //       self.addToNavbar()
    //        self.addToSubview()
            self.addToPopover()

        }

        func addToNavbar(){

            resultsViewController = GMSAutocompleteResultsViewController()
            resultsViewController?.delegate = self

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

            // Put the search bar in the navigation bar.
            searchController?.searchBar.sizeToFit()
            navigationItem.titleView = searchController?.searchBar

            // When UISearchController presents the results view, present it in
            // this view controller, not one further up the chain.
            definesPresentationContext = true

            // Prevent the navigation bar from being hidden when searching.
            searchController?.hidesNavigationBarDuringPresentation = false

        }

        func addToSubview(){
            resultsViewController = GMSAutocompleteResultsViewController()
            resultsViewController?.delegate = self

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

            let subView = UIView(frame: CGRect(x: 0, y: 65.0, width: 350.0, height: 45.0))

            subView.addSubview((searchController?.searchBar)!)
            view.addSubview(subView)
            searchController?.searchBar.sizeToFit()
            searchController?.hidesNavigationBarDuringPresentation = false

            // When UISearchController presents the results view, present it in
            // this view controller, not one further up the chain.
            definesPresentationContext = true
        }

        func addToPopover(){

            resultsViewController = GMSAutocompleteResultsViewController()
            resultsViewController?.delegate = self

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

            // Add the search bar to the right of the nav bar,
            // use a popover to display the results.
            // Set an explicit size as we don't want to use the entire nav bar.
            searchController?.searchBar.frame = (CGRect(x: 0, y: 0, width: 250.0, height: 44.0))
            navigationItem.rightBarButtonItem = UIBarButtonItem(customView: (searchController?.searchBar)!)

            // When UISearchController presents the results view, present it in
            // this view controller, not one further up the chain.
            definesPresentationContext = true

            // Keep the navigation bar visible.
            searchController?.hidesNavigationBarDuringPresentation = false
            searchController?.modalPresentationStyle = .popover
        }



        func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus)
        {
            print(status)
        }







        // Add a UIButton in Interface Builder, and connect the action to this function.
        @IBAction func getCurrentPlace(_ sender: UIButton) {

            placesClient.currentPlace(callback: { (placeLikelihoodList, error) -> Void in
                if let error = error {
                    print("Pick Place error: \(error.localizedDescription)")
                    return
                }

                self.nameLabel.text = "No current place"
                self.addressLabel.text = ""

                if let placeLikelihoodList = placeLikelihoodList {
                    print("placeLikelihoodList -- \(placeLikelihoodList)")
                    let place = placeLikelihoodList.likelihoods.first?.place
                    if let place = place {
                        self.nameLabel.text = place.name
                        self.addressLabel.text = place.formattedAddress?.components(separatedBy: ", ")
                            .joined(separator: "\n")

                        print(place.name)
                        print(place.coordinate)
                        print(place.placeID)
                        print(place.phoneNumber)
                        print(place.formattedAddress ?? "")

                    }
                }
            })
        }


    }




    //MARK: Extentions

    // Handle the user's selection.
    extension ViewController: GMSAutocompleteResultsViewControllerDelegate {
        func resultsController(_ resultsController: GMSAutocompleteResultsViewController,
                               didAutocompleteWith place: GMSPlace) {
            searchController?.isActive = false
            // Do something with the selected place.
            print("Place name: \(place.name)")
            print("Place address: \(String(describing: place.formattedAddress))")
            print("Place attributions: \(place.attributions)")
        }

        func resultsController(_ resultsController: GMSAutocompleteResultsViewController,
                               didFailAutocompleteWithError error: Error){
            // TODO: handle the error.
            print("Error: ", error.localizedDescription)
        }

        // Turn the network activity indicator on and off again.
        func didRequestAutocompletePredictions(_ viewController: GMSAutocompleteViewController) {
            UIApplication.shared.isNetworkActivityIndicatorVisible = true
        }

        func didUpdateAutocompletePredictions(_ viewController: GMSAutocompleteViewController) {
            UIApplication.shared.isNetworkActivityIndicatorVisible = false
        }
    }
Run Code Online (Sandbox Code Playgroud)


dre*_*gin 5

轻量级解决方案!

相反,使用谷歌的框架和第三方库进行简单的请求,我创建了一个简单的图书馆,在那里你可以做一些像谷歌API的请求Google AutocompleteGoogle ReverseGeoPlace InformationPath两个位置之间越来越路径API。

要使用图书馆,你所要做的就是

步骤 1 导入GoogleApiHelper到您的项目中。

step-2 初始化 GoogleApiHelper

GoogleApi.shared.initialiseWithKey("API_KEY")
Run Code Online (Sandbox Code Playgroud)

step-3 调用方法

var input = GInput()
input.keyword = "San francisco"
GoogleApi.shared.callApi(input: input) { (response) in
    if let results = response.data as? [GApiResponse.Autocomplete], response.isValidFor(.autocomplete) {
        //Enjoy the Autocomplete Api
    } else { print(response.error ?? "ERROR") }
}
Run Code Online (Sandbox Code Playgroud)

你可以在这里找到图书馆

在此处输入图片说明