Bạn có thể sử dụng SDK địa điểm dành cho Android để yêu cầu ảnh địa điểm hiển thị trong ứng dụng. Ảnh do dịch vụ ảnh trả về xuất phát từ nhiều nguồn, bao gồm cả chủ doanh nghiệp và ảnh do người dùng đóng góp. Để truy xuất hình ảnh cho một địa điểm, bạn phải thực hiện các bước sau:
- Tìm nạp đối tượng
Place
(sử dụngfetchPlace()
hoặcfindCurrentPlace()
). Hãy nhớ thêm trườngPHOTO_METADATAS
vào yêu cầu của bạn. - Trong
OnSuccessListener
choFetchPlaceRequest
, hãy thêmFetchPhotoRequest
, nếu muốn, chỉ định chiều cao và chiều rộng tối đa (tính bằng pixel). Ảnh có thể có chiều rộng hoặc chiều cao tối đa là 1600px. - Thêm một
OnSuccessListener
và lấy bitmap từFetchPhotoResponse
.
Lấy ảnh địa điểm
Ví dụ sau minh hoạ việc nhận ảnh địa điểm.
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.") } } }
Phân bổ
Trong hầu hết các trường hợp, ảnh địa điểm có thể được sử dụng mà không cần ghi nhận tác giả hoặc sẽ có thuộc tính bắt buộc là một phần của hình ảnh. Tuy nhiên, nếu bản sao PhotoMetadata
được trả về có một thuộc tính, bạn phải thêm thuộc tính bổ sung vào ứng dụng của mình ở bất cứ nơi nào bạn hiển thị hình ảnh. Để biết thêm thông tin, vui lòng xem nội dung Hiển thị mô hình phân bổ.
Mức sử dụng và thanh toán
SKU Ảnh địa điểm bị tính phí đối với các cuộc gọi đến fetchPhoto()
.
Hãy xem trang Sử dụng và
thanh toán để biết thông tin chi tiết.