You're all set!

To start developing, please head over to our developer documentation.

Activate the Google Places API for iOS

To get you started we'll guide you through the Google Developers Console to do a few things first:

  1. Create or choose a project
  2. Activate the Google Places API for iOS
  3. Create appropriate keys
Continue

Get Started with the Places API for iOS

Follow the steps in this guide to download the Google Places API for iOS, add the library and its dependencies to your app, and get a free API key.

Release notes are available for each release.

Step 1: Get the latest version of Xcode

To build a project using the Google Places API for iOS, you need version 7.3 or later of Xcode.

Step 2: Install the API

Use Cocoapods

The Google Places API for iOS is available as two CocoaPod pods. The first of these pods, GooglePlaces, contains all places funtionality which does not require a map (programmatic APIs and the autocomplete UI widget), while GooglePlacePicker is a separate pod containing a widget for searching for and selecting a place from a map.

CocoaPods is an open source dependency manager for Swift and Objective-C Cocoa projects. If you don't already have the CocoaPods tool, install it on macOS by running the following command from the terminal. For details, see the CocoaPods Getting Started guide.

sudo gem install cocoapods

Create a Podfile for the Google Places API for iOS and use it to install the API and its dependencies:

  1. If you don't have an Xcode project yet, create one now and save it to your local machine. (If you're new to iOS development, create a Single View Application.)
  2. Create a file named Podfile in your project directory. This file defines your project's dependencies.
  3. Edit the Podfile and add your dependencies. Here is an example which specifies your application target name, and the names of both pods that come with the Places API for iOS (GooglePlacePicker and GoogleMaps are only required if you are using the place picker sample):
    source 'https://github.com/CocoaPods/Specs.git'
    target 'YOUR_APPLICATION_TARGET_NAME_HERE' do
      pod 'GooglePlaces'
      pod 'GooglePlacePicker'
      pod 'GoogleMaps'
    end
  4. Save the Podfile.
  5. Open a terminal and go to the directory containing the Podfile:

    cd <path-to-project>
  6. Run the pod install command. This will install the APIs specified in the Podfile, along with any dependencies they may have.

    pod install
  7. Close Xcode, and then open (double-click) your project's .xcworkspace file to launch Xcode. From this time onwards, you must use the .xcworkspace file to open the project.

Install manually

This guide shows how to manually add the GooglePlaces framework to your project and configure your build settings in Xcode.

  1. Download the SDK source files:

    GooglePlacePicker-2.3.0 is only needed if you are using the place picker.

  2. Unpack the source files.
  3. Launch Xcode and either open an existing project, or create a new project. If you're new to iOS, create a Single View Application, and disable Use Storyboards and enable Use Automatic Reference Counting.
  4. Remove any Maps bundles from previous releases from your project.
  5. Drag the following bundles into your project (when prompted, select Copy items if needed):
    • GooglePlaces-2.3.0/Frameworks/GooglePlaces.framework
    • GoogleMaps-2.3.0/Base/Frameworks/GoogleMapsBase.framework

    If you are using the place picker, drag the following bundles into your project (in addition to the previously listed bundles):

    • GooglePlacePicker-2.3.0/Frameworks/GooglePlacePicker.framework
    • GoogleMaps-2.3.0/Maps/Frameworks/GoogleMapsCore.framework
    • GoogleMaps-2.3.0/Maps/Frameworks/GoogleMaps.framework
  6. Right-click GooglePlaces.framework in your project, and select Show In Finder.
  7. Drag the GooglePlaces.bundle from the Resources folder into your project. When prompted, ensure Copy items into destination group's folder is not selected.
  8. If you are using the place picker, repeat the previous step and drag the GoogleMaps.bundle and GooglePlacePicker.bundle files into your project. These can be found in the GoogleMaps.framework and GooglePlacePicker.framework respectively.
  9. Select your project from the Project Navigator, and choose your application's target.
  10. Open the Build Phases tab, and within Link Binary with Libraries, add the following frameworks:
    • GooglePlaces.framework
    • GoogleMapsBase.framework
    • GooglePlacePicker.framework (only if using the place picker)
    • GoogleMapsCore.framework (only if using the place picker)
    • GoogleMaps.framework (only if using the place picker)
    • Accelerate.framework
    • CoreData.framework
    • CoreGraphics.framework
    • CoreLocation.framework
    • CoreText.framework
    • GLKit.framework
    • ImageIO.framework
    • libc++.tbd
    • libz.tbd
    • OpenGLES.framework
    • QuartzCore.framework
    • SystemConfiguration.framework
    • UIKit.framework
  11. Choose your project, rather than a specific target, and open the Build Settings tab.

  12. In the Other Linker Flags section, add -ObjC. If these settings are not visible, change the filter in the Build Settings bar from Basic to All.

Step 3: Get an API key

To get started using the Google Places API for iOS, click the button below which guides you through the process of activating the Google Places API for iOS and getting an API key.

Get a Key

Alternatively, follow these steps to get an API key:

  1. Go to the Google API Console.
  2. Create or select a project.
  3. Click Continue to enable the Google Places API for iOS.
  4. On the Credentials page, get an API key.
    Note: If you have a key with iOS restrictions, you may use that key. You can use the same key with any of your iOS applications within the same project.
  5. From the dialog displaying the API key, select Restrict key to set an iOS restriction on the API key.
  6. In the Restrictions section, select iOS apps, then enter your app's bundle identifier. For example: com.example.helloplaces.
  7. Click Save.

    Your new iOS key appears in the list of API keys for your project. An API key is a string of characters, something like this:

    AIzaSyBdVl-cTICSwYKrZ95SuvNw7dbMuDt1KG0

You can also look up an existing key in the Google API Console.

For more information on using the Google API Console, see API Console Help.

Step 4: Add the API key to your application

The following code examples show how to add the API key to an application.

Swift

Add your API key to your AppDelegate.swift as follows:

  • Add the following import statement:
    import GooglePlaces
  • Add the following to your application(_:didFinishLaunchingWithOptions:) method, replacing YOUR_API_KEY with your API key:
    GMSPlacesClient.provideAPIKey("YOUR_API_KEY")
  • If you are also using the Place Picker, add your key again as shown here:
    GMSServices.provideAPIKey("YOUR_API_KEY")
Objective-C

Add your API key to your AppDelegate.m as follows:

  • Add the following import statement:
    @import GooglePlaces;
  • Add the following to your application:didFinishLaunchingWithOptions: method, replacing YOUR_API_KEY with your API key:
    [GMSPlacesClient provideAPIKey:@"YOUR_API_KEY"];
  • If you are also using the Place Picker, add your key again as shown here:
    [GMSServices provideAPIKey:@"YOUR_API_KEY"];

Step 5: Start writing code

The following code samples demonstrate how to get the current place, and add a place picker UI widget to your app.

Get current place

Swift

import UIKit
import GooglePlaces

class ViewController: UIViewController {

  var placesClient: GMSPlacesClient!

  // Add a pair of UILabels in Interface Builder, and connect the outlets to these variables.
  @IBOutlet var nameLabel: UILabel!
  @IBOutlet var addressLabel: UILabel!

  override func viewDidLoad() {
    super.viewDidLoad()
    placesClient = GMSPlacesClient.shared()
  }

  // Add a UIButton in Interface Builder, and connect the action to this function.
  @IBAction func getCurrentPlace(_ sender: UIButton) {

    placesClient.currentPlace(callback: { (placeLikelihoodList, error) -> Void in
      if let error = error {
        print("Pick Place error: \(error.localizedDescription)")
        return
      }

      self.nameLabel.text = "No current place"
      self.addressLabel.text = ""

      if let placeLikelihoodList = placeLikelihoodList {
        let place = placeLikelihoodList.likelihoods.first?.place
        if let place = place {
          self.nameLabel.text = place.name
          self.addressLabel.text = place.formattedAddress?.components(separatedBy: ", ")
            .joined(separator: "\n")
        }
      }
    })
  }
}

Objective-C

#import "ViewController.h"
@import GooglePlaces;

@interface ViewController ()
// Add a pair of UILabels in Interface Builder, and connect the outlets to these variables.
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *addressLabel;
@end

@implementation ViewController {
  GMSPlacesClient *_placesClient;
}

- (void)viewDidLoad {
  [super viewDidLoad];
  _placesClient = [GMSPlacesClient sharedClient];
}

// Add a UIButton in Interface Builder, and connect the action to this function.
- (IBAction)getCurrentPlace:(UIButton *)sender {
  [_placesClient currentPlaceWithCallback:^(GMSPlaceLikelihoodList *placeLikelihoodList, NSError *error){
    if (error != nil) {
      NSLog(@"Pick Place error %@", [error localizedDescription]);
      return;
    }

    self.nameLabel.text = @"No current place";
    self.addressLabel.text = @"";

    if (placeLikelihoodList != nil) {
      GMSPlace *place = [[[placeLikelihoodList likelihoods] firstObject] place];
      if (place != nil) {
        self.nameLabel.text = place.name;
        self.addressLabel.text = [[place.formattedAddress componentsSeparatedByString:@", "]
                                  componentsJoinedByString:@"\n"];
      }
    }
  }];
}
@end

Add a place picker

The SDK demo apps supplied with the Google Places API for iOS include a sample app for the place picker UI widget. To install the place picker demo, use the pod try GooglePlacePicker command. For more details, see the guide to code samples.

Here is a quick introductory sample for creating a place picker.

Swift

import UIKit
import GooglePlacePicker

class ViewController: UIViewController {

  // Add a pair of UILabels in Interface Builder, and connect the outlets to these variables.
  @IBOutlet var nameLabel: UILabel!
  @IBOutlet var addressLabel: UILabel!

  // Add a UIButton in Interface Builder, and connect the action to this function.
  @IBAction func pickPlace(_ sender: UIButton) {
    let center = CLLocationCoordinate2D(latitude: 37.788204, longitude: -122.411937)
    let northEast = CLLocationCoordinate2D(latitude: center.latitude + 0.001, longitude: center.longitude + 0.001)
    let southWest = CLLocationCoordinate2D(latitude: center.latitude - 0.001, longitude: center.longitude - 0.001)
    let viewport = GMSCoordinateBounds(coordinate: northEast, coordinate: southWest)
    let config = GMSPlacePickerConfig(viewport: viewport)
    let placePicker = GMSPlacePicker(config: config)

    placePicker.pickPlace(callback: {(place, error) -> Void in
      if let error = error {
        print("Pick Place error: \(error.localizedDescription)")
        return
      }

      if let place = place {
        self.nameLabel.text = place.name
        self.addressLabel.text = place.formattedAddress?.components(separatedBy: ", ")
          .joined(separator: "\n")
      } else {
        self.nameLabel.text = "No place selected"
        self.addressLabel.text = ""
      }
    })
  }
}
    

Objective-C

#import "ViewController.h"
@import GooglePlacePicker;

@interface ViewController ()
// Add a pair of UILabels in Interface Builder, and connect the outlets to these variables.
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *addressLabel;
@end

@implementation ViewController {
  GMSPlacePicker *_placePicker;
}

// Add a UIButton in Interface Builder, and connect the action to this function.
- (IBAction)pickPlace:(UIButton *)sender {

  CLLocationCoordinate2D center = CLLocationCoordinate2DMake(37.788204, -122.411937);
  CLLocationCoordinate2D northEast = CLLocationCoordinate2DMake(center.latitude + 0.001,
                                                                center.longitude + 0.001);
  CLLocationCoordinate2D southWest = CLLocationCoordinate2DMake(center.latitude - 0.001,
                                                                center.longitude - 0.001);
  GMSCoordinateBounds *viewport = [[GMSCoordinateBounds alloc] initWithCoordinate:northEast
                                                                       coordinate:southWest];
  GMSPlacePickerConfig *config = [[GMSPlacePickerConfig alloc] initWithViewport:viewport];
  _placePicker = [[GMSPlacePicker alloc] initWithConfig:config];

  [_placePicker pickPlaceWithCallback:^(GMSPlace *place, NSError *error) {
    if (error != nil) {
      NSLog(@"Pick Place error %@", [error localizedDescription]);
      return;
    }

    if (place != nil) {
      self.nameLabel.text = place.name;
      self.addressLabel.text = [[place.formattedAddress
                                 componentsSeparatedByString:@", "] componentsJoinedByString:@"\n"];
    } else {
      self.nameLabel.text = @"No place selected";
      self.addressLabel.text = @"";
    }
  }];
}
@end

Send feedback about...

location_on
Google Places API for iOS