The Google Places API for iOS is packaged with the Google Maps SDK for iOS. Before you can begin working with Google Places API for iOS, you must download the Google Maps SDK 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 6.3 or later of Xcode.
Step 2: Get CocoaPods
The Google Places API for iOS is available as a CocoaPods pod. 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 OS X by running the following command from the terminal. For details, see the CocoaPods Getting Started guide.
$ sudo gem install cocoapods
Step 3: Install the API using CocoaPods
Create a Podfile for the Google Places API for iOS and use it to
install the API and its dependencies:
- 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, and ensure that Use Automatic Reference Counting is turned on.)
- Create a file named
Podfilein your project directory. This file defines your project's dependencies, and is commonly referred to as a Podspec. - Edit the
Podfileand add your dependencies. Here is a simple Podspec, including the name of the pod you need for the Google Places API for iOS:source 'https://github.com/CocoaPods/Specs.git' platform :ios, '8.1' pod 'GoogleMaps'
- Save the
Podfile. - Open a terminal and go to the directory containing the
Podfile:$ cd <path-to-project>
- Run the
pod installcommand. This will install the APIs specified in the Podspec, along with any dependencies they may have.$ pod install
- Close Xcode, and then open (double-click) your project's
.xcworkspacefile to launch Xcode. From this time onwards, you must use the.xcworkspacefile to open the project.
Step 4: Get an API key
Follow these steps to enable the Google Places API for iOS and the Google Maps SDK for iOS in your project on the Google Developers Console. If your project doesn't already have an iOS key (a type of API key), these instructions also help you to create a new API key.
- Go to the Google Developers Console.
- Create or select a project.
- Click Continue to enable both the Google Places API for iOS and the Google Maps SDK for iOS.
- On the Credentials page, get an iOS key and set the API credentials. Note: If you have an existing iOS key, you may use that key. You can use the same key with any of your iOS applications within the same project.
- Enter your app's bundle identifier when prompted. For example:
com.example.hello-places. - Click Create. 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
For more information, see Developers Console Help.
Step 5: Add the API key to your application
The following code examples show how to add the API key to an application.
Objective-C
Add your API key to your AppDelegate.m as follows:
- Add the following import statement:
@import GoogleMaps;
- Add the following to your
application:didFinishLaunchingWithOptions:method, replacing YOUR_API_KEY with your API key:[GMSServices provideAPIKey:@"YOUR_API_KEY"];
Swift
Add your API key to your AppDelegate.swift as follows:
- Add the following import statement:
import GoogleMaps
- Add the following to your
didFinishLaunchingWithOptionsmethod, replacing YOUR_API_KEY with your API key:GMSServices.provideAPIKey("YOUR_API_KEY")
Step 6: Start writing code
For most methods in the Google Places API for iOS, the main interface is the
GMSPlacesClient
class. The place picker offers another type of interface, allowing you to add a UI widget to your app.
Below are code samples to get you started with both types of interface.
Get current place
Objective-C
#import "ViewController.h"
@import GoogleMaps;
@interface ViewController ()
// Instantiate a pair of UILabels in Interface Builder
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *addressLabel;
@end
@implementation ViewController {
GMSPlacesClient *_placesClient;
}
- (void)viewDidLoad {
[super viewDidLoad];
_placesClient = [[GMSPlacesClient alloc] init];
}
// Add a UIButton in Interface Builder to call 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
Swift
import UIKit
import GoogleMaps
class ViewController: UIViewController {
var placesClient: GMSPlacesClient?
// Instantiate a pair of UILabels in Interface Builder
@IBOutlet var nameLabel: UILabel!
@IBOutlet var addressLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
placesClient = GMSPlacesClient()
}
// Add a UIButton in Interface Builder to call this function
@IBAction func getCurrentPlace(sender: UIButton) {
placesClient?.currentPlaceWithCallback({
(placeLikelihoodList: GMSPlaceLikelihoodList?, error: NSError?) -> 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!.componentsSeparatedByString(", ")
.joinWithSeparator("\n")
}
}
})
}
}
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. Try the SDK demos using pod try GoogleMaps.
For more details, see the guide to code samples.
Here is a quick introductory sample for creating a place picker.
Objective-C
#import "ViewController.h"
@import GoogleMaps;
@interface ViewController ()
// Instantiate a pair of UILabels in Interface Builder
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *addressLabel;
@end
@implementation ViewController {
GMSPlacePicker *_placePicker;
}
// Add a UIButton in Interface Builder to call 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
Swift
import UIKit
import GoogleMaps
class ViewController: UIViewController {
var placePicker: GMSPlacePicker?
// Instantiate a pair of UILabels in Interface Builder
@IBOutlet var nameLabel: UILabel!
@IBOutlet var addressLabel: UILabel!
// Add a UIButton in Interface Builder to call this function
@IBAction func pickPlace(sender: UIButton) {
let center = CLLocationCoordinate2DMake(37.788204, -122.411937)
let northEast = CLLocationCoordinate2DMake(center.latitude + 0.001, center.longitude + 0.001)
let southWest = CLLocationCoordinate2DMake(center.latitude - 0.001, center.longitude - 0.001)
let viewport = GMSCoordinateBounds(coordinate: northEast, coordinate: southWest)
let config = GMSPlacePickerConfig(viewport: viewport)
placePicker = GMSPlacePicker(config: config)
placePicker?.pickPlaceWithCallback({ (place: GMSPlace?, error: NSError?) -> 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!.componentsSeparatedByString(", ").joinWithSeparator("\n")
} else {
self.nameLabel.text = "No place selected"
self.addressLabel.text = ""
}
})
}
}