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

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

  1. โทรหา [GMSPlacesClient fetchPlaceFromPlaceId] โดยส่งสตริงที่มีรหัสสถานที่และฟังก์ชันเรียกกลับ ซึ่งจะเรียกใช้การเรียกกลับ ด้วยออบเจ็กต์ GMSPlacePhotoMetadataList
  2. ในออบเจ็กต์ GMSPlacePhotoMetadataList ให้เข้าถึงพร็อพเพอร์ตี้ results แล้วเลือกรูปภาพที่จะโหลดจากอาร์เรย์
  3. สำหรับแต่ละ GMSPlacePhotoMetadata หากต้องการโหลดจากรายการนี้ ให้โทรหา [GMSPlacesClient loadPlacePhoto:callback:] หรือ [GMSPlacesClient loadPlacePhoto:constrainedToSize:scale:callback:] ซึ่งจะเรียกใช้การเรียกกลับด้วย UIImage ที่ใช้ได้ รูปภาพมีความกว้างหรือความสูงได้สูงสุด 1,600 พิกเซล

โค้ดตัวอย่าง

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

Swift

// Specify the place data types to return (in this case, just photos).
let fields: GMSPlaceField = GMSPlaceField(rawValue: UInt(GMSPlaceField.photos.rawValue))!

placesClient?.fetchPlace(fromPlaceID: "INSERT_PLACE_ID_HERE",
                         placeFields: fields,
                         sessionToken: nil, callback: {
  (place: GMSPlace?, error: Error?) in
  if let error = error {
    print("An error occurred: \(error.localizedDescription)")
    return
  }
  if let place = place {
    // Get the metadata for the first photo in the place photo metadata list.
    let photoMetadata: GMSPlacePhotoMetadata = place.photos![0]

    // Call loadPlacePhoto to display the bitmap and attribution.
    self.placesClient?.loadPlacePhoto(photoMetadata, callback: { (photo, error) -> Void in
      if let error = error {
        // TODO: Handle the error.
        print("Error loading photo metadata: \(error.localizedDescription)")
        return
      } else {
        // Display the first image and its attributions.
        self.imageView?.image = photo;
        self.lblText?.attributedText = photoMetadata.attributions;
      }
    })
  }
})

Objective-C

// Specify the place data types to return (in this case, just photos).
GMSPlaceField fields = (GMSPlaceFieldPhotos);

NSString *placeId = @"INSERT_PLACE_ID_HERE";

[_placesClient fetchPlaceFromPlaceID:placeId placeFields:fields sessionToken:nil callback:^(GMSPlace * _Nullable place, NSError * _Nullable error) {
  if (error != nil) {
    NSLog(@"An error occurred %@", [error localizedDescription]);
    return;
  }
  if (place != nil) {
    GMSPlacePhotoMetadata *photoMetadata = [place photos][0];
    [self->_placesClient loadPlacePhoto:photoMetadata callback:^(UIImage * _Nullable photo, NSError * _Nullable error) {
      if (error != nil) {
        NSLog(@"Error loading photo metadata: %@", [error localizedDescription]);
        return;
      } else {
        // Display the first image and its attributions.
        self->imageView.image = photo;
        self->lblText.attributedText = photoMetadata.attributions;
      }
    }];
  }
}];

การแคช

[GMSPlacesClient loadPlacePhoto:callback:] หรือ [GMSPlacesClient loadPlacePhoto:constrainedToSize:scale:callback:] จะแคชรูปภาพที่โหลดโดยใช้ทั้งในดิสก์และในหน่วยความจำโดยระบบการโหลด URL ของ Foundation ใน NSURLCache ที่แชร์

หากต้องการกำหนดค่าลักษณะการทำงานของการแคช คุณสามารถเปลี่ยนแคช URL ที่แชร์ได้โดยใช้ [NSURLCache setSharedURLCache:] ในเมธอด application:didFinishLaunchingWithOptions: ของตัวแทนแอปพลิเคชัน

หากไม่ต้องการให้แอปพลิเคชันแชร์ NSURLCache กับ Places SDK สำหรับ iOS คุณสามารถสร้าง NSURLCache ใหม่และใช้ NSURLCache นี้เฉพาะภายในแอปโดยไม่ต้องตั้งค่าเป็นแคชที่แชร์

การระบุแหล่งที่มา

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

ขีดจำกัดการใช้งาน

การดึงข้อมูลรูปภาพจะใช้โควต้า 1 หน่วย แต่ไม่มีขีดจำกัดการใช้งานสำหรับการดึงข้อมูลเมตาของรูปภาพ อ่านเพิ่มเติมเกี่ยวกับการใช้งานและการเรียกเก็บเงิน