場所の写真

プラットフォームを選択: Android iOS JavaScript ウェブサービス

Places SDK for Android を使用すると、アプリケーションに表示する場所の写真をリクエストできます。フォトサービスで返される写真は、ビジネス オーナーやユーザーが投稿した写真など、さまざまなソースから取得されます。

Places SDK for Android は、最大サイズが 1,600 x 1,600 ピクセルのビットマップ画像を返します。

写真取得プロセス

場所の画像を取得するには:

  1. Place Details を使用して Place オブジェクトを取得します(fetchPlace() または findCurrentPlace() を使用します)。レスポンスの Place オブジェクトに含めるフィールドのリストには、必ず Place.Field PHOTO_METADATAS フィールドを含めます。
  2. FetchPlaceResponse または FindCurrentPlaceResponseOnSuccessListenerPlace.getPhotoMetadas() を使用して、レスポンス Place オブジェクトからタイプ PhotoMetadata の写真メタデータ オブジェクトを取得します。
  3. FetchPhotoRequest オブジェクトを作成し、必要に応じて最大の高さと幅(ピクセル単位)を指定します。写真の幅と高さの上限は 1,600 ピクセルです。
  4. PlacesClient.fetchPhoto() を使用して、写真のビットマップをリクエストします。
  5. OnSuccessListener を追加し、FetchPhotoResponse から写真を取得します。

写真を取得

次の例は、場所の写真を取得する方法を示しています。

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 タイプの写真メタデータ オブジェクトには、次の 2 種類の追加属性のいずれかを含めることができます。

返された PhotoMetadata オブジェクトにいずれかのタイプの帰属情報が含まれている場合は、画像を表示するすべての箇所に、その帰属情報を含める必要があります。詳細については、アトリビューションの表示をご覧ください。

使用量と請求額

fetchPhoto() を呼び出すと、Places Photo SKU が課金されます。詳細については、使用量と請求額のページをご覧ください。