Places SDK for Android を使用して、アプリケーションに表示する場所の写真をリクエストできます。フォトサービスから返される写真の提供元はさまざまです。ビジネス オーナーやユーザーが投稿した写真などが該当します。場所の画像を取得するには、次の手順を行う必要があります。
Place
オブジェクトを取得します(fetchPlace()
またはfindCurrentPlace()
を使用します)。リクエストには、必ずPHOTO_METADATAS
フィールドを含めてください。FetchPlaceRequest
のOnSuccessListener
にFetchPhotoRequest
を追加し、必要に応じて最大の高さと幅(ピクセル単位)を指定します。写真の最大幅または高さは 1,600 ピクセルです。OnSuccessListener
を追加し、FetchPhotoResponse
からビットマップを取得します。
場所の写真を取得
次の写真は、場所の写真を取得する方法を示しています。
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. } }); });
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.") } } }
帰属表示
ほとんどの場合、プレイスの写真は帰属情報なしで使用できます。また、必要な帰属情報が画像の一部として含まれている場合もあります。ただし、返された PhotoMetadata
インスタンスにアトリビューションが含まれている場合は、画像を表示するすべての箇所で、追加のアトリビューションをアプリケーションに含める必要があります。詳細については、アトリビューションを表示するをご覧ください。
使用量と請求額
fetchPhoto()
の呼び出しでは、Places Photo SKU が課金されます。詳しくは、使用量と請求額のページをご覧ください。