Places SDK for Android を使用して、アプリケーションで表示する場所の写真をリクエストできます。写真サービスから返される写真は、ビジネス オーナーやユーザーが投稿した写真など、さまざまなソースから返されます。
Places SDK for Android は、最大サイズが 1,600 x 1,600 ピクセルのビットマップ画像を返します。
写真の取得プロセス
場所の画像を取得するには:
- Place Details を使用して
Place
オブジェクトを取得します(fetchPlace()
またはfindCurrentPlace()
を使用します)。レスポンスのPlace
オブジェクトに含めるフィールドのリストには、Place.Field PHOTO_METADATAS
フィールドを含めてください。 FetchPlaceResponse
またはFindCurrentPlaceResponse
のOnSuccessListener
でPlace.getPhotoMetadas()
を使用して、レスポンスPlace
オブジェクトからPhotoMetadata
型の写真メタデータ オブジェクトを取得します。FetchPhotoRequest
オブジェクトを作成し、必要に応じて最大の高さと幅(ピクセル単位)を指定します。写真の幅または高さは最大 1,600 ピクセルです。PlacesClient.fetchPhoto()
を使用して写真ビットマップをリクエストします。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 種類の追加の帰属情報のいずれかを含めることができます。
- Attributions:
PhotoMetadata.getAttributions()
がアクセスするアトリビューション文字列。 - AuthorAttributions。
PhotoMetadata.getAuthorAttributions()
がアクセスするAuthorAttributions
オブジェクト。
返された PhotoMetadata
オブジェクトにいずれかのタイプのアトリビューションが含まれている場合は、画像を表示するすべての場所に、そのアトリビューションを含める必要があります。詳細については、アトリビューションの表示をご覧ください。
使用量と請求額
fetchPhoto()
を呼び出すと、Places Photo SKU が課金されます。詳細については、使用量と課金ページをご覧ください。