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 Photos

You can use the Google Places API for iOS to request place photos to display in your application. Photos returned by the photos service come from a variety of sources, including business owners and user-contributed photos. To retrieve images for a place, you must take the following steps:

  1. Call [GMSPlacesClient lookUpPhotosForPlaceID:callback:], passing a string with a place ID and a callback. This will call the callback with a GMSPlacePhotoMetadataList object.
  2. On the GMSPlacePhotoMetadataList object access the results property and select the photos to load from the array.
  3. For each GMSPlacePhotoMetadata to load from this list call [GMSPlacesClient loadPlacePhoto:callback:] or [GMSPlacesClient loadPlacePhoto:constrainedToSize:scale:callback:]. These will call the callback with a usable UIImage.

Sample code

The following example method takes a place ID and gets the first photo in the returned list. You can use this method as a template for the method you will create in your own app.

Swift

func loadFirstPhotoForPlace(placeID: String) {
  GMSPlacesClient.shared().lookUpPhotos(forPlaceID: placeID) { (photos, error) -> Void in
    if let error = error {
      // TODO: handle the error.
      print("Error: \(error.localizedDescription)")
    } else {
      if let firstPhoto = photos?.results.first {
        self.loadImageForMetadata(photoMetadata: firstPhoto)
      }
    }
  }
}

func loadImageForMetadata(photoMetadata: GMSPlacePhotoMetadata) {
  GMSPlacesClient.shared().loadPlacePhoto(photoMetadata, callback: {
    (photo, error) -> Void in
      if let error = error {
        // TODO: handle the error.
        print("Error: \(error.localizedDescription)")
      } else {
        self.imageView.image = photo;
        self.attributionTextView.attributedText = photoMetadata.attributions;
      }
  })
}

Objective-C

- (void)loadFirstPhotoForPlace:(NSString *)placeID {
  [[GMSPlacesClient sharedClient]
      lookUpPhotosForPlaceID:placeID
                    callback:^(GMSPlacePhotoMetadataList *_Nullable photos,
                               NSError *_Nullable error) {
                      if (error) {
                        // TODO: handle the error.
                        NSLog(@"Error: %@", [error description]);
                      } else {
                        if (photos.results.count > 0) {
                          GMSPlacePhotoMetadata *firstPhoto = photos.results.firstObject;
                          [self loadImageForMetadata:firstPhoto];
                        }
                      }
                    }];
}

- (void)loadImageForMetadata:(GMSPlacePhotoMetadata *)photoMetadata {
  [[GMSPlacesClient sharedClient]
         loadPlacePhoto:photoMetadata
      constrainedToSize:self.imageView.bounds.size
                  scale:self.imageView.window.screen.scale
               callback:^(UIImage *_Nullable photo, NSError *_Nullable error) {
                 if (error) {
                   // TODO: handle the error.
                   NSLog(@"Error: %@", [error description]);
                 } else {
                   self.imageView.image = photo;
                   self.attributionTextView.attributedText = photoMetadata.attributions;
                 }
               }];
}

Caching

Photos loaded using [GMSPlacesClient loadPlacePhoto:callback:] or [GMSPlacesClient loadPlacePhoto:constrainedToSize:scale:callback:] are cached both on disk and in-memory by the Foundation URL loading system in the shared NSURLCache.

To configure the caching behavior you can change the shared URL cache using [NSURLCache setSharedURLCache:] in your application delegate's application:didFinishLaunchingWithOptions: method.

If you do not want your application to share a NSURLCache with the Google Places API for iOS you can create a new NSURLCache and use this exclusively within your app without setting it as the shared cache.

Attributions

In most cases, place photos can be used without attribution, or will have the required attribution included as part of the image. However, if the returned GMSPlacePhotoMetadata instance includes an attribution, you must include the additional attribution in your application wherever you display the image. Note that links in the attribution must be tappable. See the documentation on attributions.

Usage limits

Retrieving an image costs one unit of quota; there are no usage limits for retrieving photo metadata. See the documentation on usage limits.

Send feedback about...

location_on
Google Places API for iOS