我想在“文本”字段中显示“ Google自动放置”。 我写了以下代码,但我无法理解我在哪里给apikey。 同时,我也获得了选定地址的纬度和经度。
import UIKit
import GoogleMaps
import GooglePlaces
class ViewController: UIViewController ,UITextFieldDelegate,GMSAutocompleteViewControllerDelegate{
@IBOutlet weak var placeaddress: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
let apikey = "API_KEY"
self.placeaddress.delegate = self
}
func textFieldDidBeginEditing(_ textField: UITextField) {
let acController = GMSAutocompleteViewController()
acController.delegate = self
self.present(acController, animated: true, completion: nil)
}
// 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)
}
}
答案 0 :(得分:0)
首先将Google Framework导入appDelegate
import GoogleMaps
之后,将您的api密钥提供给appDelegate中的didFinishLaunchingWithOptions
GMSServices.provideAPIKey("Your API Key")
希望这会对您有所帮助。