使用Swift在UItextField中使用Google地方自动填充功能

时间:2018-02-13 17:06:55

标签: swift uitextfield google-places-api

我想在UITextfield下方添加Google地方自动填充搜索建议,我想要用户的经度和纬度。将有两个文本字段,即拾取位置和下落位置。例如附上图片。我尝试过代码,但它没有在uitextfield下面显示。

import UIKit
import GoogleMaps
import GooglePlaces
import GooglePlacePicker

class ViewController: UIViewController, UITextFieldDelegate {

@IBOutlet weak var pickupLocation: UITextField!

override func viewDidLoad() {
    super.viewDidLoad()

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

extension ViewController: GMSAutocompleteViewControllerDelegate {

// Handle the user's selection.
func viewController(_ viewController: GMSAutocompleteViewController, didAutocompleteWith place: GMSPlace) {
    print("Place name: \(place.name)")
    print("Place address: \(String(describing: place.formattedAddress))")
    print("Place attributions: \(String(describing: place.attributions))")
    dismiss(animated: true, completion: nil)
}

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

// User canceled the operation.
func wasCancelled(_ viewController: GMSAutocompleteViewController) {
    dismiss(animated: true, completion: nil)
}

示例图片如下  enter image description here

0 个答案:

没有答案