swift3 - 在 Swift 3 中单击 textField 时在 tableView 上搜索 Google Places?

标签 swift3 google-places

当我点击 textField 并希望在 textField 下方显示结果时,如何搜索 Google 地方信息。我正在使用 Swift 3.0 版本。有人可以帮助我

最佳答案

首先,您需要在项目中安装 GooglePlaces SDK。然后你引用下面的代码。

在 Swift 3 中

在 AppDelegate 中

import UIKit
import CoreData
import GoogleMaps
import GooglePlacePicker

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

func application(_ application: UIApplication, didFinishLaunchingWithOptions 
launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    func setUpGoogleMaps()
    return true
}

func setUpGoogleMaps() {
    let googleMapsApiKey = "AIzaSyA-kiOBvrR9CNztqutwmKaSLyXIid93K0E"
    GMSServices.provideAPIKey(googleMapsApiKey)
    GMSPlacesClient.provideAPIKey("AIzaSyA-kiOBvrR9CNztqutwmKaSLyXIid93K0E")
}

在具有文本字段的 ViewController 中

import UIKit
import GooglePlaces
import GoogleMaps
import GooglePlacePicker

class HotelVC: UIViewController, GMSMapViewDelegate, UITextFieldDelegate {

@IBOutlet weak var YourTextField: UITextField!

override func viewDidLoad() {
    super.viewDidLoad()
    self.YourTextField.delegate = self

}

func textFieldDidBeginEditing(_ textField: UITextField) {
    let acController = GMSAutocompleteViewController()
    acController.delegate = self
    self.present(acController, animated: true, completion: nil)
  }
}


extension viewController: GMSAutocompleteViewControllerDelegate {

func viewController(_ viewController: GMSAutocompleteViewController, didAutocompleteWith place: GMSPlace) {

    print("Place name: \(place.name)")
    print("Place address: \(place.formattedAddress ?? "null")")
    self.YourTextField.text = place.formattedAddress
    print("Place attributions: \(String(describing: place.attributions))")

    self.dismiss(animated: true, completion: nil)
}
func viewController(_ viewController: GMSAutocompleteViewController, didFailAutocompleteWithError error: Error) {
    // TODO: handle the error.
    //        print("Error: \(error.description)")
    self.dismiss(animated: true, completion: nil)
}

// User canceled the operation.
func wasCancelled(_ viewController: GMSAutocompleteViewController) {
    print("Autocomplete was cancelled.")
    self.dismiss(animated: true, completion: nil)
    }
}

关于swift3 - 在 Swift 3 中单击 textField 时在 tableView 上搜索 Google Places?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45854332/

相关文章:

ios - 此 IP、站点或移动应用程序无权使用此 API key 。如何解决 iOS 应用程序的此问题?

Android Google Places API - PlaceAutocompleteFragment 清除按钮监听器

timer - 使用 Swift 3 停止预定定时器,即使定时器为零,定时器也会继续触发

push-notification - xcode 8 beta ios 10 推送通知停止工作

javascript - 从 OSM 中删除地点

Android Google 将地点自动完成 View 设置为自动关闭

swift - Swift 3 的二元运算符

ios - 快速禁用 UITextField 中的闪烁光标?

arrays - Swift Realm [[String]] 对象

android - 在 Android 中使用 Google places API 添加您自己的位置时添加描述