地點相片

您可以使用 Places SDK for iOS 要求要顯示在 應用程式相簿服務傳回的相片來自多種 ,包括業主和使用者提供的相片。擷取 張貼某個地點的相片,請務必按照下列步驟進行:

  1. 呼叫 [GMSPlacesClient fetchPlaceFromPlaceId]。 傳遞包含地點 ID 和回呼的字串。這會呼叫回呼 使用 GMSPlacePhotoMetadataList 物件。
  2. GMSPlacePhotoMetadataList 物件存取 results 屬性,並選取要從陣列載入的相片。
  3. 針對每個 GMSPlacePhotoMetadata 即可從這份清單呼叫 [GMSPlacesClient loadPlacePhoto:callback:] 載入 或 [GMSPlacesClient loadPlacePhoto:constrainedToSize:scale:callback:]。 這些方法會使用可用的 UIImage 來呼叫回呼。相片上限為 寬度或高度設為 1600 像素

程式碼範例

以下範例方法會擷取地點 ID,並取得 。您可以使用此方法做為範本, 建立自己的應用程式

Swift

// Specify the place data types to return (in this case, just photos).
let fields: GMSPlaceField = GMSPlaceField(rawValue: UInt(GMSPlaceField.photos.rawValue))!

placesClient?.fetchPlace(fromPlaceID: "INSERT_PLACE_ID_HERE",
                         placeFields: fields,
                         sessionToken: nil, callback: {
  (place: GMSPlace?, error: Error?) in
  if let error = error {
    print("An error occurred: \(error.localizedDescription)")
    return
  }
  if let place = place {
    // Get the metadata for the first photo in the place photo metadata list.
    let photoMetadata: GMSPlacePhotoMetadata = place.photos![0]

    // Call loadPlacePhoto to display the bitmap and attribution.
    self.placesClient?.loadPlacePhoto(photoMetadata, callback: { (photo, error) -> Void in
      if let error = error {
        // TODO: Handle the error.
        print("Error loading photo metadata: \(error.localizedDescription)")
        return
      } else {
        // Display the first image and its attributions.
        self.imageView?.image = photo;
        self.lblText?.attributedText = photoMetadata.attributions;
      }
    })
  }
})

Objective-C

// Specify the place data types to return (in this case, just photos).
GMSPlaceField fields = (GMSPlaceFieldPhotos);

NSString *placeId = @"INSERT_PLACE_ID_HERE";

[_placesClient fetchPlaceFromPlaceID:placeId placeFields:fields sessionToken:nil callback:^(GMSPlace * _Nullable place, NSError * _Nullable error) {
  if (error != nil) {
    NSLog(@"An error occurred %@", [error localizedDescription]);
    return;
  }
  if (place != nil) {
    GMSPlacePhotoMetadata *photoMetadata = [place photos][0];
    [self->_placesClient loadPlacePhoto:photoMetadata callback:^(UIImage * _Nullable photo, NSError * _Nullable error) {
      if (error != nil) {
        NSLog(@"Error loading photo metadata: %@", [error localizedDescription]);
        return;
      } else {
        // Display the first image and its attributions.
        self->imageView.image = photo;
        self->lblText.attributedText = photoMetadata.attributions;
      }
    }];
  }
}];

快取

已使用 [GMSPlacesClient loadPlacePhoto:callback:] 載入相片 或 [GMSPlacesClient loadPlacePhoto:constrainedToSize:scale:callback:] 基礎網址載入系統會在磁碟和記憶體內快取 在分享的 NSURLCache 中。

如要設定快取行為,您可以使用 [NSURLCache setSharedURLCache:]敬上 在應用程式委派的 application:didFinishLaunchingWithOptions: 中 方法。

如果您不希望應用程式將 NSURLCache 與 Places SDK for iOS 可以建立新的 NSURLCache, 單獨使用的應用程式,而不設為共用快取。

歸因

在大多數情況下,使用地點相片時不必註明出處, 在圖片中加入所需的作者資訊。不過,如果傳回 GMSPlacePhotoMetadata敬上 就包含屬性時,您必須額外加入 在應用程式中顯示圖片請注意, 出處必須可輕觸請參閱作者資訊說明文件。

用量限制

擷取映像檔會耗用一個配額單位;沒有使用限制 正在擷取相片中繼資料。進一步瞭解 用量與計費