Follow the steps in this guide to download the Places SDK for iOS, to add the library and its dependencies to your app, and to get a free API key.
Release notes are available for each release.
Prior to using the Places SDK for iOS, do the following:
- Follow the Get an API Key guide to get, add, and restrict an API key.
- Enable billing on each of your projects.
- Enable the Places API for each of your projects.
Step 1: Get the latest version of Xcode
To build a project using the Places SDK for iOS, you need version 9.0 or later of Xcode.
Step 2: Install the SDK
To install the API in a new project, follow these steps:Use Cocoapods
The Places SDK for iOS is available as two CocoaPod pods. The first of these pods, GooglePlaces, contains all places functionality 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 Places SDK for iOS and use
it to install the SDK 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.)
- Create a file named
Podfilein your project directory. This file defines your project's dependencies. - Edit the
Podfileand add your dependencies. Here is an example which specifies your application target name, and the names of both pods that come with the Places SDK for iOS (GooglePlacePickerandGoogleMapsare 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 'GoogleMaps' end
- 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 thePodfile, 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.
Install manually
This guide shows how to manually add the GooglePlaces framework to your project and configure your build settings in Xcode.
- Download the SDK source files:
- Unpack the source files.
- 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.
- Remove any Maps bundles from previous releases from your project.
- Drag the following bundles into your project (when prompted, select
Copy items if needed):
GooglePlaces-3.x.x/Frameworks/GooglePlaces.frameworkGoogleMaps-3.x.x/Base/Frameworks/GoogleMapsBase.framework
- Right-click
GooglePlaces.frameworkin your project, and select Show In Finder. - Drag the
GooglePlaces.bundlefrom theResourcesfolder into your project. When prompted, ensure Copy items into destination group's folder is not selected. - Select your project from the Project Navigator, and choose your application's target.
- Open the Build Phases tab, and within Link Binary with
Libraries, add the following frameworks:
GooglePlaces.frameworkGoogleMapsBase.frameworkAccelerate.frameworkCoreData.frameworkCoreGraphics.frameworkCoreImage.frameworkCoreLocation.frameworkCoreTelephony.frameworkCoreText.frameworkGLKit.frameworkImageIO.frameworklibc++.tbdlibz.tbdOpenGLES.frameworkQuartzCore.frameworkSystemConfiguration.frameworkUIKit.framework
Choose your project, rather than a specific target, and open the Build Settings tab.
- 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.
To update the API for an existing project, follow these steps:
- Open a terminal and go to the project directory containing the
Podfile. - Run the
pod updatecommand. This will update all of the APIs specified in thePodfileto the latest version.
Step 3: Start writing code
The following code samples demonstrate how to get the current place.
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("Current 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(@"Current 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