รูปภาพสถานที่

เลือกแพลตฟอร์ม: Android iOS JavaScript บริการเว็บ

คุณสามารถใช้ Places SDK สำหรับ Android เพื่อขอรูปภาพสถานที่ที่จะแสดงในแอปพลิเคชันของคุณ รูปภาพที่ส่งมาจากบริการรูปภาพนั้นมาจากแหล่งที่มาที่หลากหลาย ทั้งเจ้าของธุรกิจและรูปภาพที่ผู้ใช้ส่งมา

Places SDK สำหรับ Android แสดงผลรูปภาพบิตแมปที่มีขนาดสูงสุด 1,600 x 1600 พิกเซล

กระบวนการเรียกดูรูปภาพ

วิธีเรียกรูปภาพของสถานที่

  1. ใช้รายละเอียดสถานที่เพื่อดึงข้อมูลออบเจ็กต์ Place (ใช้ fetchPlace() หรือ findCurrentPlace()) อย่าลืมรวมช่อง Place.Field PHOTO_METADATAS ไว้ในรายการช่องที่จะรวมไว้ในออบเจ็กต์ Place ของคำตอบ
  2. ใน OnSuccessListener สำหรับ FetchPlaceResponse หรือ FindCurrentPlaceResponse ใช้ Place.getPhotoMetadas() เพื่อรับออบเจ็กต์ข้อมูลเมตาของรูปภาพประเภท PhotoMetadata จากออบเจ็กต์ Place ตอบกลับ
  3. สร้างออบเจ็กต์ FetchPhotoRequest คุณจะเลือกระบุความสูงและความกว้างสูงสุด (เป็นพิกเซล) หรือไม่ก็ได้ โดยรูปภาพจะมีความกว้างหรือความสูงได้สูงสุดที่ 1,600 พิกเซล
  4. ใช้ PlacesClient.fetchPhoto() เพื่อขอบิตแมปรูปภาพ
  5. เพิ่ม 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 ประเภทดังนี้

หากออบเจ็กต์ PhotoMetadata ที่แสดงผลมีแอตทริบิวต์ประเภทใดประเภทหนึ่ง คุณจะต้องใส่การระบุแหล่งที่มาในแอปพลิเคชันของคุณด้วยทุกที่ที่แสดงรูปภาพนั้น สำหรับข้อมูลเพิ่มเติม ดูการแสดงการระบุแหล่งที่มา

การใช้งานและการเรียกเก็บเงิน

ระบบจะเรียกเก็บเงิน SKU ของ Places Photo สำหรับการโทรไปยัง fetchPhoto() ดูรายละเอียดได้ที่หน้าการใช้งานและการเรียกเก็บเงิน