我想在显示文本字段的自动谷歌的地方。我写了下面的代码,但我无法理解在我给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)
}
}
首先导入谷歌框架到的appDelegate
import GoogleMaps
在此之后提供API密钥到的appDelegate didFinishLaunchingWithOptions
GMSServices.provideAPIKey("Your API Key")
希望这会帮助你。