地點相片

您可以使用 Places SDK for Android 要求要在應用程式中顯示的地點相片。「相片」服務傳回的相片取自各種來源,包括業主和使用者提供的相片。 如要擷取地點的圖片,必須執行下列步驟:

  1. 使用 Place Details 擷取 Place 物件 (使用 fetchPlace()findCurrentPlace())。請務必在欄位清單中加入 Place.Field PHOTO_METADATAS 欄位,以便加入回應 Place 物件。
  2. FetchPlaceResponseFindCurrentPlaceResponseOnSuccessListener 中:
    1. 使用 Place.getPhotoMetadas() 從回應 Place 物件取得 PhotoMetadata 類型的相片中繼資料物件。
    2. 建立 FetchPhotoRequest 物件,可選擇指定高度和寬度上限 (以像素為單位)。相片寬度或高度上限為 1600 像素。
    3. 使用 PlacesClient.fetchPhoto() 要求相片。
    4. 新增 OnSuccessListener 並從 FetchPhotoResponse 取得相片。

存取版本 3.3.0 中新增的 PhotoMetadata 資料

Places SDK for Android (新) 會將 AuthorAttributions 欄位新增至 PhotoMetadata 類別。如果應用程式啟用新的 SDK,Place.getPhotoMetadas() 傳回的 PhotoMetadata 物件可能包含一或多個作者歸因。

如果 PhotoMetadata 物件包含任何作者資訊,無論是 3.3.0 版新增的新作者作者資訊,還是 3.2.0 以下版本提供的現有作者資訊,都必須隨相片一併顯示。如要進一步瞭解如何處理所有類型的歸因,請參閱「歸因」一文。

如要在 PhotoMetadata 物件中填入作者作者資訊,您必須:

  1. 設定 Google Cloud 專案時啟用新的 SDK。
  2. 在活動或片段內初始化新的 SDK
  3. 在 Place Details 要求的欄位清單中加入 Place.Field.PHOTO_METADATAS
  4. 呼叫 PlacesClient.fetchPlace() 取得 Place 物件,呼叫 Place.getPhotoMetadas() 以取得 PhotoMetadata 物件。PlacesClient.findCurrentPlace() 不支援作者作者資訊欄位。
  5. 使用 PhotoMetadata.getAuthorAttributions() 取得作者作者資訊。

取得地點相片

下例示範如何取得地點相片。

Kotlin



// Define a Place ID.
val placeId = "INSERT_PLACE_ID_HERE"

// Specify fields. Requests for photos must always have the PHOTO_METADATAS field.
val fields = listOf(Place.Field.PHOTO_METADATAS)

// Get a Place object (this example uses fetchPlace(), but you can also use findCurrentPlace())
val placeRequest = FetchPlaceRequest.newInstance(placeId, fields)

placesClient.fetchPlace(placeRequest)
    .addOnSuccessListener { response: FetchPlaceResponse ->
        val place = response.place

        // Get the photo metadata.
        val metada = place.photoMetadatas
        if (metada == null || metada.isEmpty()) {
            Log.w(TAG, "No photo metadata.")
            return@addOnSuccessListener
        }
        val photoMetadata = metada.first()

        // Get the attribution text.
        val attributions = photoMetadata?.attributions

        // Create a FetchPhotoRequest.
        val photoRequest = FetchPhotoRequest.builder(photoMetadata)
            .setMaxWidth(500) // Optional.
            .setMaxHeight(300) // Optional.
            .build()
        placesClient.fetchPhoto(photoRequest)
            .addOnSuccessListener { fetchPhotoResponse: FetchPhotoResponse ->
                val bitmap = fetchPhotoResponse.bitmap
                imageView.setImageBitmap(bitmap)
            }.addOnFailureListener { exception: Exception ->
                if (exception is ApiException) {
                    Log.e(TAG, "Place not found: " + exception.message)
                    val statusCode = exception.statusCode
                    TODO("Handle error with given status code.")
                }
            }
    }

      

Java


// Define a Place ID.
final String placeId = "INSERT_PLACE_ID_HERE";

// Specify fields. Requests for photos must always have the PHOTO_METADATAS field.
final List<Place.Field> fields = Collections.singletonList(Place.Field.PHOTO_METADATAS);

// Get a Place object (this example uses fetchPlace(), but you can also use findCurrentPlace())
final FetchPlaceRequest placeRequest = FetchPlaceRequest.newInstance(placeId, fields);

placesClient.fetchPlace(placeRequest).addOnSuccessListener((response) -> {
    final Place place = response.getPlace();

    // Get the photo metadata.
    final List<PhotoMetadata> metadata = place.getPhotoMetadatas();
    if (metadata == null || metadata.isEmpty()) {
        Log.w(TAG, "No photo metadata.");
        return;
    }
    final PhotoMetadata photoMetadata = metadata.get(0);

    // Get the attribution text.
    final String attributions = photoMetadata.getAttributions();

    // Create a FetchPhotoRequest.
    final FetchPhotoRequest photoRequest = FetchPhotoRequest.builder(photoMetadata)
        .setMaxWidth(500) // Optional.
        .setMaxHeight(300) // Optional.
        .build();
    placesClient.fetchPhoto(photoRequest).addOnSuccessListener((fetchPhotoResponse) -> {
        Bitmap bitmap = fetchPhotoResponse.getBitmap();
        imageView.setImageBitmap(bitmap);
    }).addOnFailureListener((exception) -> {
        if (exception instanceof ApiException) {
            final ApiException apiException = (ApiException) exception;
            Log.e(TAG, "Place not found: " + exception.getMessage());
            final int statusCode = apiException.getStatusCode();
            // TODO: Handle error with given status code.
        }
    });
});

      

歸因

在大多數情況下,地點相片可在沒有作者資訊的情況下使用,或會在圖片中加入必要的作者資訊。不過,PhotoMetadata 類型的相片中繼資料物件可以包含以下兩種額外歸因類型之一:

如果傳回的 PhotoMetadata 物件包含任一類型的歸因,則每次顯示圖片時都必須在應用程式中加入作者資訊。詳情請參閱「顯示歸因」一文。

用量與計費

針對向 fetchPhoto() 發出的呼叫,系統會視為 Places Photo SKU 來計費。詳情請參閱用量與計費頁面。