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

Place Picker

The place picker UI widget

The place picker is a simple and yet flexible built-in UI widget, part of the Google Places API for iOS.

Introducing the place picker

The GMSPlacePickerViewController provides a UI that displays an interactive map and a list of nearby places, including places corresponding to geographical addresses and local businesses. Users can choose a place, and your app can then retrieve the details of the selected place.

The place picker provides the following advantages over developing your own UI widget:

  1. The user experience is consistent with other apps using the place picker, including Google apps and third parties. This means users of your app already know how to interact with the place picker.
  2. The map is integrated into the place picker.
  3. Accessibility is built in.
  4. It saves development time.

The place picker features autocomplete functionality, which displays place predictions based on user search input. This functionality is present in all place picker integrations, so you don't need to do anything extra to enable autocomplete. For more information about autocomplete, see Place Autocomplete.

Request location authorization

If your app uses the place picker, you must request permission to use location services. First add one or both of the following keys to your Info.plist file, to request 'when in use' or 'always' authorization:

  • NSLocationWhenInUseUsageDescription
  • NSLocationAlwaysUsageDescription

For the place picker, it's enough to request 'when in use' authorization, but you may want to request 'always' authorization for other functionality in your app. For each key, add a string informing the user why you need the location services. For example:

<key>NSLocationWhenInUseUsageDescription</key>
<string>Show your location on the map</string>

Add a place picker

The code snippet below shows how to create a GMSPlacePickerViewController centered at the current device location, and display details of the selected place.

Swift

// The code snippet below shows how to create and display a GMSPlacePickerViewController.
@IBAction func pickPlace(_ sender: UIButton) {
  let config = GMSPlacePickerConfig(viewport: nil)
  let placePicker = GMSPlacePickerViewController(config: config)

  present(placePicker, animated: true, completion: nil)
}

// To receive the results from the place picker 'self' will need to conform to
// GMSPlacePickerViewControllerDelegate and implement this code.
func placePicker(_ viewController: GMSPlacePickerViewController, didPick place: GMSPlace) {
  // Dismiss the place picker, as it cannot dismiss itself.
  viewController.dismiss(animated: true, completion: nil)

  print("Place name \(place.name)")
  print("Place address \(place.formattedAddress)")
  print("Place attributions \(place.attributions)")
}

func placePickerDidCancel(_ viewController: GMSPlacePickerViewController) {
  // Dismiss the place picker, as it cannot dismiss itself.
  viewController.dismiss(animated: true, completion: nil)

  print("No place selected")
}

Objective-C

// The code snippet below shows how to create and display a GMSPlacePickerViewController.
- (IBAction)pickPlace:(UIButton *)sender {
  GMSPlacePickerConfig *config = [[GMSPlacePickerConfig alloc] initWithViewport:nil];
  GMSPlacePickerViewController *placePicker =
    [[GMSPlacePickerViewController alloc] initWithConfig:config];
  placePicker.delegate = self;

  [self presentViewController:placePicker animated:YES completion:nil];
}

// To receive the results from the place picker 'self' will need to conform to
// GMSPlacePickerViewControllerDelegate and implement this code.
- (void)placePicker:(GMSPlacePickerViewController *)viewController didPickPlace:(GMSPlace *)place {
  // Dismiss the place picker, as it cannot dismiss itself.
  [viewController dismissViewControllerAnimated:YES completion:nil];

  NSLog(@"Place name %@", place.name);
  NSLog(@"Place address %@", place.formattedAddress);
  NSLog(@"Place attributions %@", place.attributions.string);
}

- (void)placePickerDidCancel:(GMSPlacePickerViewController *)viewController {
  // Dismiss the place picker, as it cannot dismiss itself.
  [viewController dismissViewControllerAnimated:YES completion:nil];

  NSLog(@"No place selected");
}

As shown in the above code sample, you can initalize the place picker with a given configuration using a GMSPlacePickerConfig object. If a value of nil is assigned to the viewport, the place picker will center on the current device location. To center on a specific location, specify a viewport containing a GMSCoordinateBounds object defining the initial rectangular map area to display. The following example shows creating a GMSCoordinateBounds to center the map on Sydney, Australia.

Swift

let center = CLLocationCoordinate2D(latitude: -33.865143, longitude: 151.2099)
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)

Objective-C

CLLocationCoordinate2D center = CLLocationCoordinate2DMake(-33.865143, 151.2099);
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];

To be notified when the user selects a place you must set the delegate of the place picker to be an object implementing GMSPlacePickerViewControllerDelegate. When you do this your app will receive a callback to the placePicker(_:didPick:). method implemented by the delegate and is passed the place the user selected. If the user did not select a place, placePickerDidCancel(_:) is called instead.

As the place picker is a normal view controller it can be displayed any way you want. For example, in a popover, fullscreen, pushed onto a navigation stack, or even as part of a custom app UI. Because of this flexibility the place picker is unable to dismiss itself, so your app must programmatically dismiss it when placePicker(_:didPick:) is called. In some cases it may be necessary to dismiss the place picker from placePickerDidCancel(_:) as well.

Display attributions in your app

When your app displays information obtained via the place picker, the app must also display attributions. See the documentation on attributions.

Old Client Libraries

Prior to version 2.3 of the Google Places API for iOS GMSPlacePicker was the only available way to use the place picker. This class has now been deprecated and it is recommended that GMSPlacePickerViewController is used instead. This new class is more flexible and is not limited to being displayed full-screen.

Send feedback about...

location_on
Google Places API for iOS