คุณสามารถใช้ Places SDK สำหรับ Android เพื่อขอให้รูปภาพสถานที่แสดงในแอปพลิเคชันของคุณ รูปภาพที่มีบริการรูปภาพนั้นมาจากแหล่งที่มาหลากหลาย ซึ่งรวมถึงเจ้าของธุรกิจและรูปภาพจากผู้ใช้ หากต้องการเรียกรูปภาพของสถานที่ คุณต้องทำตามขั้นตอนต่อไปนี้
- ใช้รายละเอียดสถานที่เพื่อดึงข้อมูลออบเจ็กต์
Place
(ใช้fetchPlace()
หรือfindCurrentPlace()
) อย่าลืมใส่ช่องPlace.Field PHOTO_METADATAS
ในรายการช่องที่จะรวมไว้ในออบเจ็กต์Place
ของการตอบกลับด้วย - ใน
OnSuccessListener
สำหรับFetchPlaceResponse
หรือFindCurrentPlaceResponse
ให้ทำดังนี้- ใช้
Place.getPhotoMetadas()
เพื่อรับออบเจ็กต์ข้อมูลเมตาของรูปภาพประเภทPhotoMetadata
จากออบเจ็กต์Place
ของการตอบกลับ - สร้างออบเจ็กต์
FetchPhotoRequest
โดยอาจระบุความสูงและความกว้างสูงสุด (เป็นพิกเซล) หรือไม่ก็ได้ รูปภาพมีความกว้างหรือความสูงไม่เกิน 1,600 พิกเซล - ใช้
PlacesClient.fetchPhoto()
เพื่อขอรูปภาพ - เพิ่ม
OnSuccessListener
และรับรูปภาพจากFetchPhotoResponse
- ใช้
เข้าถึงข้อมูล PhotoMetadata ที่เพิ่มในเวอร์ชัน 3.3.0
Places SDK สำหรับ Android (ใหม่) จะเพิ่มช่อง AuthorAttributions
ลงในคลาส PhotoMetadata
หากแอปเปิดใช้ SDK ใหม่ ออบเจ็กต์ PhotoMetadata
ที่ Place.getPhotoMetadas()
แสดงผลจะมีการระบุแหล่งที่มาของผู้เขียนได้อย่างน้อย 1 รายการ
เมื่อออบเจ็กต์ PhotoMetadata
มีการระบุแหล่งที่มาทั้งหมด ไม่ว่าจะเป็นการระบุแหล่งที่มาใหม่ของผู้แต่งที่เพิ่มในเวอร์ชัน 3.3.0 หรือการระบุแหล่งที่มาที่มีอยู่ในเวอร์ชัน 3.2.0 ขึ้นไป คุณต้องแสดงการระบุแหล่งที่มาเหล่านั้นพร้อมกับรูปภาพ สำหรับข้อมูลเพิ่มเติมเกี่ยวกับการจัดการการระบุแหล่งที่มาทุกประเภท
ดูการระบุแหล่งที่มา
หากต้องการเติมข้อมูลออบเจ็กต์ PhotoMetadata
พร้อมการระบุแหล่งที่มาของผู้เขียน คุณต้องทำดังนี้
- เปิดใช้ SDK ใหม่เมื่อคุณตั้งค่าโปรเจ็กต์ Google Cloud
- เริ่มต้น SDK ใหม่ภายในกิจกรรมหรือส่วนย่อย
- ใส่
Place.Field.PHOTO_METADATAS
ในรายการช่องของคำขอรายละเอียดสถานที่ - เรียก
PlacesClient.fetchPlace()
เพื่อรับออบเจ็กต์Place
และPlace.getPhotoMetadas()
เพื่อรับออบเจ็กต์PhotoMetadata
PlacesClient.findCurrentPlace()
ไม่รองรับช่องการระบุแหล่งที่มาของผู้เขียน - ใช้
PhotoMetadata.getAuthorAttributions()
เพื่อดูการระบุแหล่งที่มาของผู้เขียน
ขอรูปภาพสถานที่
ตัวอย่างต่อไปนี้แสดงถึงการรับรูปภาพสถานที่
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 ประเภทดังนี้
- การระบุแหล่งที่มา สตริงการระบุแหล่งที่มาที่
PhotoMetadata.getAttributions()
เข้าถึง - AuthorAttributions ซึ่งเป็นออบเจ็กต์
AuthorAttributions
ที่เข้าถึงได้โดยPhotoMetadata.getAuthorAttributions()
หากออบเจ็กต์ PhotoMetadata
ที่แสดงผลมีการระบุแหล่งที่มาประเภทใดก็ตาม คุณต้องรวมการระบุแหล่งที่มาไว้ในแอปพลิเคชันด้วยทุกที่ที่แสดงรูปภาพ ดูข้อมูลเพิ่มเติมได้ที่การแสดงการระบุแหล่งที่มา
การใช้งานและการเรียกเก็บเงิน
จะมีการเรียกเก็บเงิน SKU ของ Places Photo สำหรับการโทรหา fetchPhoto()
โปรดดูรายละเอียดที่หน้าการใช้งานและการเรียกเก็บเงิน