您可以使用 Places SDK for iOS (新版) 請求地點相片,在應用程式中顯示。「地點相片」服務傳回的相片取自各種來源,包括業主和使用者提供的相片。
相片是點陣圖,以 UIImage 物件表示。點陣圖圖片的大小上限為 4800 x 4800 像素。
要求圖片
每個地點最多可要求 10 張相片:
呼叫
[GMSPlacesClient lookUpPhotosForPlaceID]
,並傳遞地點 ID 和GMSPlacePhotoMetadataResultCallback
回呼。這項要求會使用GMSPlacePhotoMetadataList
物件呼叫GMSPlacePhotoMetadataResultCallback
回呼。回呼中的
GMSPlacePhotoMetadataList
物件會包含相片,每張相片都以GMSPlacePhotoMetadata
物件表示,並儲存在results
陣列屬性中。使用
GMSPlacePhotoMetadata
物件建立GMSFetchPhotoRequest
,包括所要求圖片的最大尺寸。針對陣列中的每個
GMSPlacePhotoMetadata
物件,呼叫[GMSPlacesClient fetchPhotoWithRequest:callback:]
傳遞GMSFetchPhotoRequest
物件。這個方法會使用可用的點陣圖圖片做為 UIImage,呼叫GMSFetchPhotoResultCallback
回呼。
如要要求地點相片,也可以發出 Place Details (New) 要求,並在欄位清單中加入 GMSPlacePropertyPhotos
。呼叫 Place Details 的優點是,回應 GMSPlace
物件可以包含相片,以及您想取得的地點資料欄位。
程式碼範例
下列範例方法會取得地點 ID,並取得傳回清單中的第一張相片。您可以在自己的應用程式中建立方法,並以這個方法做為範本。
Places Swift SDK
// First fetch place details // A hotel in Saigon with an attribution. let placeID = "ChIJV4k8_9UodTERU5KXbkYpSYs" let fetchPlaceRequest = FetchPlaceRequest( placeID: placeID, placeProperties: [ . name, .website ] ) var fetchedPlace: Place switch await placesClient.fetchPlace(with: fetchPlaceRequest) { case .success(let place): fetchedPlace = place case .failure(let placesError): // Handle error } // Use the place details to fetch a photo's image. guard let photo = fetchedPlace.photos?.first else { // Handle place without photos. } let fetchPhotoRequest = FetchPhotoRequest(photo: photo, maxSize: CGSizeMake(4800, 4800)) switch await placesClient.fetchPhoto(with: fetchPhotoRequest) { case .success(let uiImage): // Handle image. case .failure(let placesError): // Handle error }
Swift
// A hotel in Saigon with an attribution. let placeID = "ChIJV4k8_9UodTERU5KXbkYpSYs" // Request list of photos for a place placesClient.lookUpPhotos(forPlaceID: placeID) { (photos, error) in guard let photoMetadata: GMSPlacePhotoMetadata = photos?.results[0] else { return } // Request individual photos in the response list let fetchPhotoRequest = GMSFetchPhotoRequest(photoMetadata: photoMetadata, maxSize: CGSizeMake(4800, 4800)) self.client.fetchPhoto(with: fetchPhotoRequest, callback: { (photoImage: UIImage?, error: Error?) in guard let photoImage, error == nil else { print("Handle photo error: ") return } print("Display photo Image: ") } ) }
Objective-C
// A hotel in Saigon with an attribution. NSString *placeID = @"ChIJV4k8_9UodTERU5KXbkYpSYs"; [placesClient lookUpPhotosForPlaceID:placeID callback: ^(GMSPlacePhotoMetadataList *list, NSError *error) { GMSPlacePhotoMetadata *photoMetadata = [list results][0]; // Request individual photos in the response list GMSFetchPhotoRequest *fetchPhotoRequest = [[GMSFetchPhotoRequest alloc] initWithPhotoMetadata:photoMetadata maxSize:CGSizeMake(4800, 4800)]; [placesClient fetchPhotoWithRequest:fetchPhotoRequest callback: ^(UIImage *_Nullable photoImage, NSError *_Nullable error) { if (error == nil) { // Display photo } }]; }];
快取
使用 [GMSPlacesClient loadPlacePhoto:callback:]
或 [GMSPlacesClient loadPlacePhoto:constrainedToSize:scale:callback:]
載入的相片,會由共用 NSURLCache
中的 Foundation URL 載入系統,快取到磁碟和記憶體中。
如要設定快取行為,您可以在應用程式委派的 application:didFinishLaunchingWithOptions:
方法中,使用 [NSURLCache setSharedURLCache:]
變更共用 URL 快取。
如果您不希望應用程式與 Places SDK for iOS 共用 NSURLCache
,可以建立新的 NSURLCache
,並在應用程式中專門使用這個 NSURLCache
,而不將其設為共用快取。
歸因
在大多數情況下,使用地點相片時可以不包含作者資訊,圖片本身也可能已加入必要的作者資訊。不過,如果傳回的 GMSPlacePhotoMetadata
執行個體包含任何 attributions
或 authorAttribution
,您每次顯示圖片時就必須在應用程式中加入這些出處資訊。請參閱出處資訊說明文件。