Places SDK for Android を使用すると、アプリケーションに表示する場所の写真をリクエストできます。フォトサービスで返される写真は、ビジネス オーナーやユーザーが投稿した写真など、さまざまなソースから取得されます。
写真の形式を選択する
Places SDK for Android では、リクエストされた写真に対して次の 2 つの形式がサポートされています。
- Places SDK for Android のすべてのバージョン: ビットマップ画像を返します。ビットマップ画像のサイズの上限は 1,600 x 1,600 ピクセルです。
- Places SDK for Android(新規)バージョン 3.4 以降: ビットマップ画像の URI を返します。ビットマップ画像のサイズの上限は 4,800×4,800 ピクセルです。
写真取得プロセス
場所の画像を取得するには:
- Place Details を使用して
Place
オブジェクトを取得します(fetchPlace()
またはfindCurrentPlace()
を使用します)。レスポンスのPlace
オブジェクトに含めるフィールドのリストには、必ずPlace.Field PHOTO_METADATAS
フィールドを含めます。 FetchPlaceResponse
またはFindCurrentPlaceResponse
のOnSuccessListener
で次の操作を行います。Place.getPhotoMetadas()
を使用して、レスポンスのPlace
オブジェクトからPhotoMetadata
型の写真メタデータ オブジェクトを取得します。- ビットマップ画像を取得するには:
FetchPhotoRequest
オブジェクトを作成し、必要に応じて最大の高さと幅(ピクセル単位)を指定します。写真の幅と高さの上限は 1,600 ピクセルです。PlacesClient.fetchPhoto()
を使用して、写真のビットマップをリクエストします。OnSuccessListener
を追加し、FetchPhotoResponse
から写真を取得します。
- 写真の URI を取得するには:
- リクエストを行う
FetchResolvedPhotoUriRequest
オブジェクトを作成します。写真の幅と高さの上限は 4,800 ピクセルです。 PlacesClient.fetchResolvedPhotoUri()
を使用して、写真の URI をリクエストします。OnSuccessListener
を追加し、FetchResolvedPhotoUriResponse
オブジェクトから写真の URI を取得します。
- リクエストを行う
バージョン 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()
を使用します。
写真を取得
このセクションでは、写真をビットマップまたは URI として取得する方法について説明します。
場所の写真をビットマップとして取得する
次の例は、場所の写真をビットマップとして取得する方法を示しています。
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. } }); });
場所の写真の URI を取得する
次の例は、場所の写真の URI を取得する方法を示しています。
// 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 FetchResolvedPhotoUriRequest. final FetchResolvedPhotoUriRequest photoRequest = FetchResolvedPhotoUriRequest.builder(photoMetadata) .setMaxWidth(500) // Optional. .setMaxHeight(300) // Optional. .build(); // Request the photo URI placesClient.fetchResolvedPhotoUri(photoRequest).addOnSuccessListener((fetchResolvedPhotoUriResponse) -> { Uri uri = fetchResolvedPhotoUriResponse.getUri(); RequestOptions requestOptions = new RequestOptions().override(Target.SIZE_ORIGINAL); Glide.with(this).load(uri).apply(requestOptions).into(imageView); }).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 種類の追加属性のいずれかを含めることができます。
- Attributions:
PhotoMetadata.getAttributions()
がアクセスするアトリビューション文字列。 - AuthorAttributions:
PhotoMetadata.getAuthorAttributions()
によってアクセスされるAuthorAttributions
オブジェクト。
返された PhotoMetadata
オブジェクトにいずれかのタイプの帰属情報が含まれている場合は、画像を表示するすべての箇所に、その帰属情報を含める必要があります。詳細については、アトリビューションの表示をご覧ください。
使用量と請求額
fetchPhoto()
を呼び出すと、Places Photo SKU が課金されます。詳細については、使用量と請求額のページをご覧ください。