-2

I want display the Google Automatic places in Text field. I write the following code but I unable to understand where the I give apikey. Same time I getting the latitude and longitude also for selected address.

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)
    }
}
  • Why havn't you read the [doc](https://developers.google.com/places/ios-sdk/get-api-key) – dahiya_boy Feb 11 '19 at 08:02
  • I make those also even my app is crashed and showing the error like: The operation couldn’t be completed. The Places API for iOS is not enabled. See the developer's guide (https://developers.google.com/places/ios-api/start) for how to enable the Google Places API for iOS. – Srikanth Thirumalasetty Feb 11 '19 at 10:04

1 Answers1

0

First import Google Framework into appDelegate

import GoogleMaps

After that provide your api key into didFinishLaunchingWithOptions in appDelegate

GMSServices.provideAPIKey("Your API Key")

Hope this will help you.

Shiv Kumar
  • 932
  • 7
  • 12
  • App is crashed with following error *** Terminating app due to uncaught exception 'GMSPlacesException', reason: 'Google Places API for iOS must be initialized via [GMSPlacesClient provideAPIKey:...] prior to use' – Srikanth Thirumalasetty Feb 11 '19 at 08:00
  • Ok, Use this GMSPlacesClient.provideAPIKey("YOUR_API_KEY") and replace GMSServices.provideAPIKey("Your API Key") – Shiv Kumar Feb 11 '19 at 08:45
  • Thank you I facing problem like following I already enable the places how to resolve The operation couldn’t be completed. The Places API for iOS is not enabled. See the developer's guide (https://developers.google.com/places/ios-api/start) for how to enable the Google Places API for iOS. – Srikanth Thirumalasetty Feb 11 '19 at 08:54