รูปภาพสถานที่ (เดิม)

นักพัฒนาซอฟต์แวร์ในเขตเศรษฐกิจยุโรป (EEA)

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

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

กระบวนการดึงข้อมูลรูปภาพ

วิธีดึงรูปภาพสำหรับสถานที่

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

รับรูปภาพ

ตัวอย่างต่อไปนี้แสดงการรับรูปภาพสถานที่

Kotlin

// Define a Place ID.
val placeId = PlaceIdProvider.getRandomPlaceId()

// 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 metadata = place.photoMetadatas
        if (metadata == null || metadata.isEmpty()) {
            Log.w(TAG, "No photo metadata.")
            return@addOnSuccessListener
        }
        val photoMetadata = metadata.first()

        // Get the attribution text.
        val attributions = photoMetadata?.attributions

        binding.placePhotosAttributions.text = attributions

        // Create a FetchResolvedPhotoUriRequest.
        val photoRequest = FetchResolvedPhotoUriRequest.builder(photoMetadata)
            .setMaxWidth(500) // Optional.
            .setMaxHeight(300) // Optional.
            .build()
        placesClient.fetchResolvedPhotoUri(photoRequest)
            .addOnSuccessListener { fetchPhotoResponse ->
                val photoUri = fetchPhotoResponse.uri
                Glide.with(this)
                    .load(photoUri)
                    .into(binding.placePhotosResult)
            }.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 = PlaceIdProvider.getRandomPlaceId();

// 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();
    binding.placePhotosAttributions.setText(attributions);

    // Create a FetchResolvedPhotoUriRequest.
    final FetchResolvedPhotoUriRequest photoRequest = FetchResolvedPhotoUriRequest.builder(photoMetadata)
        .setMaxWidth(500) // Optional.
        .setMaxHeight(300) // Optional.
        .build();
    placesClient.fetchResolvedPhotoUri(photoRequest).addOnSuccessListener((fetchPhotoResponse) -> {
        Glide.with(this)
                .load(fetchPhotoResponse.getUri())
                .into(binding.placePhotosResult);
    }).addOnFailureListener((exception) -> {
        if (exception instanceof ApiException) {
            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 ของPlace Photos (เดิม) สำหรับการเรียกใช้ fetchPhoto() ดูรายละเอียดได้ที่หน้าการใช้งานและการเรียกเก็บเงิน