如何自动获得谷歌的地方

问题描述 投票:-2回答:1

我想在显示文本字段的自动谷歌的地方。我写了下面的代码,但我无法理解在我给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)
    }
}
ios swift google-maps google-places
1个回答
0
投票

首先导入谷歌框架到的appDelegate

import GoogleMaps

在此之后提供API密钥到的appDelegate didFinishLaunchingWithOptions

GMSServices.provideAPIKey("Your API Key")

希望这会帮助你。

© www.soinside.com 2019 - 2024. All rights reserved.