Places SDK for Android を使用すると、アプリに表示する場所の写真のリクエストを行うことができます。フォトサービスによって返される写真の提供元は、お店やサービスの所有者、ユーザーの投稿などさまざまです。
Places SDK for Android は、最大サイズが 1,600 x 1,600 ピクセルのビットマップ画像を返します。
写真の取得プロセス
場所の画像を取得するには:
- 場所の詳細を使用して
Place
オブジェクトを取得します(fetchPlace()
またはfindCurrentPlace()
を使用)。レスポンスのPlace
オブジェクトに含めるフィールドのリストに、Place.Field PHOTO_METADATAS
フィールドを含めるようにしてください。 FetchPlaceResponse
またはFindCurrentPlaceResponse
のOnSuccessListener
で、Place.getPhotoMetadas()
を使用して、レスポンスPlace
オブジェクトからPhotoMetadata
型のフォト メタデータ オブジェクトを取得します。FetchPhotoRequest
オブジェクトを作成し、必要に応じて最大の高さと幅(ピクセル単位)を指定します。写真の幅または高さは最大 1,600 ピクセルです。PlacesClient.fetchPhoto()
を使用して、写真のビットマップをリクエストします。OnSuccessListener
を追加して、FetchPhotoResponse
から写真を取得します。
写真を取得する
次の例は、プレイスの写真の取得を示しています。
// 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.") } } }
// 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 が課金されます。詳細については、使用量と課金のページをご覧ください。