地點相片

您可以使用 Places SDK for iOS 要求在應用程式中顯示的地點相片。相片服務傳回的相片來自各種來源,包括業主和使用者提供的相片。如要擷取某個地點的相片,您必須按照下列步驟操作:

  1. 呼叫 [GMSPlacesClient fetchPlaceFromPlaceId],傳遞含有地點 ID 和回呼的字串。這會使用 GMSPlacePhotoMetadataList 物件呼叫回呼。
  2. GMSPlacePhotoMetadataList 物件上存取 results 屬性,並選取要從陣列載入的相片。
  3. 針對每個要從這份清單呼叫 [GMSPlacesClient loadPlacePhoto:callback:][GMSPlacesClient loadPlacePhoto:constrainedToSize:scale:callback:] 載入的 GMSPlacePhotoMetadata。這些方法會使用可用的 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 中的基礎網址載入系統,在磁碟和記憶體中快取。

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

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

出處

在大多數情況下,使用地點相片時無須註明出處,或者圖片會包含必要的作者資訊。不過,如果傳回的 GMSPlacePhotoMetadata 執行個體包含作者資訊,您就必須在應用程式中為顯示圖片的任何位置另外加入作者資訊。請注意,作者資訊中的連結必須可輕觸。請參閱作者資訊說明文件。

用量限制

擷取圖片的費用是單一單位的配額;擷取相片中繼資料則沒有使用限制。進一步瞭解用量與計費