New pricing changes went into effect on July 16, 2018. For more information, check out the Guide for Existing Users.

Address Form Autocomplete

This tutorial shows you how to use the autocomplete UI control to return a list of potential addresses in response to a user query, and populate a form with the resulting address data. Follow this tutorial to build an iOS app using the Places SDK for iOS.

Get the code

Clone or download the Google Maps iOS samples repository from GitHub.

Set up your development project

Follow these steps to install the Places SDK for iOS and the Maps SDK for iOS:

  1. Download and install Xcode.
  2. If you don't already have the CocoaPods tool, install it on macOS by running the following command from the terminal:
    sudo gem install cocoapods
  3. Clone or download the Google Maps iOS samples repository.
  4. Navigate to the places-address-form directory, where the project's Podfile is located.
  5. Run the pod install command. This will install the APIs specified in the Podfile, along with any dependencies they may have.
  6. Open places-address-form.xcworkspace in Xcode.

Get an API key and enable the necessary APIs

To complete this tutorial, you need a Google API key that's authorized to use the Places SDK for iOS. Click the button below to get a key and activate the API.

Get a Key

For more details, see the guide to getting an API key.

Add the API key to your application

In your AppDelegate.swift, add the following to your application(_:didFinishLaunchingWithOptions:) method, replacing YOUR_API_KEY with your API key:


Build and run your app

  1. Connect an iOS device to your computer, or select a simulator from the Xcode scheme pop-up menu.
  2. In Xcode, click the Product/Run menu option (or the play button icon).

Xcode builds the app, and then runs the app on the device or on the simulator.

You should see an address form, and a button for launching the autocomplete UI control, similar to the image on this page. Tap the button, enter an address, select an autocomplete result, and the results appear in the address form.

Understand the code

This part of the tutorial explains the most significant parts of the places-address-form app, to help you understand how to build a similar app.

Implement the autocomplete UI control

This app implements the autocomplete UI control as a full-screen control. Implement the GMSAutocompleteViewControllerDelegate protocol in the main view controller by using an extension clause, and add the required GMSAutocompleteViewController methods within the extension.

extension ViewController: GMSAutocompleteViewControllerDelegate {

  // Handle the user's selection.
  func viewController(_ viewController: GMSAutocompleteViewController, didAutocompleteWith place: GMSPlace) {
    // Print place info to the console.
    print("Place name: \(")
    print("Place address: \(place.formattedAddress)")
    print("Place attributions: \(place.attributions)")

    // TODO: Add code to get address components from the selected place.

    // Close the autocomplete widget.
    dismiss(animated: true, completion: nil)

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

  func wasCancelled(_ viewController: GMSAutocompleteViewController) {
    dismiss(animated: true, completion: nil)

  // Show the network activity indicator.
  func didRequestAutocompletePredictions(_ viewController: GMSAutocompleteViewController) {
    UIApplication.shared.isNetworkActivityIndicatorVisible = true

  // Hide the network activity indicator.
  func didUpdateAutocompletePredictions(_ viewController: GMSAutocompleteViewController) {
    UIApplication.shared.isNetworkActivityIndicatorVisible = false


Display the autocomplete UI control

The autocomplete UI control is displayed when the user taps the UIButton. This function initializes the autocomplete UI control, sets the GMSAutocompleteFilter to return only results with a precise address, and finally displays the autocomplete view controller. In this example, this function is part of the main view controller class:

// Present the Autocomplete view controller when the button is pressed.
@IBAction func autocompleteClicked(_ sender: UIButton) {
  let autocompleteController = GMSAutocompleteViewController()
  autocompleteController.delegate = self

  // Set a filter to return only addresses.
  let filter = GMSAutocompleteFilter()
  filter.type = .address
  autocompleteController.autocompleteFilter = filter

  present(autocompleteController, animated: true, completion: nil)

About address data

You can get address data from a GMSPlace instance in two ways:

  • By using the formattedAddress property to get a string containing the place's full address.
  • By using the addressComponents property to get an array of GMSAddressComponent objects representing the components in the place's address.

The Places SDK for iOS returns the following types of address components:

  • street_number indicates the numbered portion of a precise street address.
  • route indicates the actual street name of a precise street address.
  • neighborhood indicates the name of the local town or community.
  • locality indicates an incorporated city or town political entity.
  • administrative_area_level_1 indicates a first-order civil entity below the country level. Within the United States, these administrative levels are states. Not all nations exhibit these administrative levels. In most cases, administrative_area_level_1 short names will closely match ISO 3166-2 subdivisions and other widely circulated lists; however this is not guaranteed as our results are based on a variety of signals and location data.
  • administrative_area_level_2 indicates a second-order civil entity below the country level. Within the United States, these administrative levels are counties. Not all nations exhibit these administrative levels.
  • country indicates the national political entity, and is typically the highest order type that is returned.
  • postal_code indicates a postal code as used to address postal mail within the country.
  • postal_code_suffix indicates the suffix for a postal code.

See Place Types for a complete list of types returned by the Places service.

Get address data from the response

Handle the response in the didAutocompleteWithPlace member function in the GMSAutocompleteViewControllerDelegate extension:

// Handle the user's selection.
func viewController(_ viewController: GMSAutocompleteViewController, didAutocompleteWith place: GMSPlace) {
  // Print place info to the console.
  print("Place name: \(")
  print("Place address: \(place.formattedAddress)")
  print("Place attributions: \(place.attributions)")

  // Get the address components.
  if let addressLines = place.addressComponents {
    // Populate all of the address fields we can find.
    for field in addressLines {
      switch field.type {
      case kGMSPlaceTypeStreetNumber:
        street_number =
      case kGMSPlaceTypeRoute:
        route =
      case kGMSPlaceTypeNeighborhood:
        neighborhood =
      case kGMSPlaceTypeLocality:
        locality =
      case kGMSPlaceTypeAdministrativeAreaLevel1:
        administrative_area_level_1 =
      case kGMSPlaceTypeCountry:
        country =
      case kGMSPlaceTypePostalCode:
        postal_code =
      case kGMSPlaceTypePostalCodeSuffix:
        postal_code_suffix =
      // Print the items we aren't using.
        print("Type: \(field.type), Name: \(")

  // Call custom function to populate the address form.

  // Close the autocomplete widget.
  self.dismiss(animated: true, completion: nil)

This function gets all of the address components needed to construct an address, and prints any unused address components to the console.

Populate the address form

The last step is adding all of the address values to each UITextField in the address form.

// Populate the address form fields.
func fillAddressForm() {
  address_line_1.text = street_number + " " + route
  city.text = locality
  state.text = administrative_area_level_1
  if postal_code_suffix != "" {
    postal_code_field.text = postal_code + "-" + postal_code_suffix
  } else {
    postal_code_field.text = postal_code
  country_field.text = country

  // Clear values for next time.
  street_number = ""
  route = ""
  neighborhood = ""
  locality = ""
  administrative_area_level_1  = ""
  country = ""
  postal_code = ""
  postal_code_suffix = ""

Congratulations! You've built an iOS app that uses the autocomplete UI control to get an address, and populates a form with the results. You've also learned how to use the Places SDK for iOS.

다음에 대한 의견 보내기...