Places SDK for Android を使用すると、アプリケーションに表示する場所の写真をリクエストできます。フォトサービスから返される写真の提供元は、ビジネス オーナーやユーザーが投稿した写真などさまざまです。 場所の画像を取得する手順は次のとおりです。
- Place Details を使用して
Place
オブジェクトを取得します(fetchPlace()
またはfindCurrentPlace()
を使用します)。レスポンスのPlace
オブジェクトに含めるフィールドのリストには、必ずPlace.Field PHOTO_METADATAS
フィールドを含めます。 FetchPlaceResponse
またはFindCurrentPlaceResponse
のOnSuccessListener
で、次の操作を行います。Place.getPhotoMetadas()
を使用して、レスポンスPlace
オブジェクトからPhotoMetadata
型の写真メタデータ オブジェクトを取得します。FetchPhotoRequest
オブジェクトを作成し、必要に応じて最大の高さと幅(ピクセル単位)を指定します。写真の幅と高さの上限は 1,600 ピクセルです。PlacesClient.fetchPhoto()
を使用して写真をリクエストします。OnSuccessListener
を追加し、FetchPhotoResponse
から写真を取得します。
バージョン 3.3.0 で追加された PhotoMetadata データにアクセスする
Places SDK for Android(新規)では、AuthorAttributions
フィールドが PhotoMetadata
クラスに追加されます。アプリで新しい SDK を有効にしている場合、Place.getPhotoMetadas()
によって返される PhotoMetadata
オブジェクトに 1 つ以上の作成者属性を含めることができます。
PhotoMetadata
オブジェクトに属性(バージョン 3.3.0 で追加された新しい作成者の帰属、またはバージョン 3.2.0 以前で利用可能な既存の帰属)が含まれている場合は、それらを写真とともに表示する必要があります。すべてのタイプのアトリビューションの処理の詳細については、アトリビューションをご覧ください。
PhotoMetadata
オブジェクトに作成者の属性を設定するには、次のことを行う必要があります。
- Google Cloud プロジェクトをセットアップするときに、新しい SDK を有効にします。
- アクティビティまたはフラグメント内で新しい SDK を初期化します。
- Place Details リクエストのフィールド リストに
Place.Field.PHOTO_METADATAS
を含めます。 PlacesClient.fetchPlace()
を呼び出してPlace
オブジェクトを取得し、Place.getPhotoMetadas()
を呼び出してPhotoMetadata
オブジェクトを取得します。作成者属性フィールドは、PlacesClient.findCurrentPlace()
ではサポートされていません。- 著者の帰属情報を取得するには、
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
タイプの写真メタデータ オブジェクトには、次の 2 種類の追加属性のいずれかを含めることができます。
- アトリビューション。
PhotoMetadata.getAttributions()
によってアクセスされるアトリビューション文字列。 - AuthorAttributions:
PhotoMetadata.getAuthorAttributions()
がアクセスするAuthorAttributions
オブジェクト。
返された PhotoMetadata
オブジェクトにいずれかのタイプの帰属情報が含まれている場合は、アプリケーション内の画像を表示するすべての箇所に帰属情報を含める必要があります。詳細については、アトリビューションの表示をご覧ください。
使用量と請求額
fetchPhoto()
を呼び出すと、Places Photo SKU が課金されます。詳細については、使用量と課金のページをご覧ください。