Bạn có thể sử dụng Places SDK for iOS (Mới) để yêu cầu ảnh địa điểm hiển thị trong ứng dụng của mình. Ảnh do dịch vụ ảnh trả về đến từ nhiều nguồn, bao gồm cả ảnh do chủ doanh nghiệp và người dùng đóng góp.
Ảnh là hình ảnh bitmap do đối tượng UIImage biểu thị.
Hình ảnh bitmap có kích thước tối đa là 4800 x 4800 pixel.
Yêu cầu tạo hình ảnh
Bạn có thể yêu cầu tối đa 10 bức ảnh cho một địa điểm:
Từ đối tượng GMSPlacePhotoMetadataList trong lệnh gọi lại, thuộc tính mảng results chứa các bức ảnh, trong đó mỗi bức ảnh được biểu thị bằng một đối tượng GMSPlacePhotoMetadata.
Sử dụng đối tượng GMSPlacePhotoMetadata để tạo GMSFetchPhotoRequest, bao gồm cả kích thước tối đa của hình ảnh được yêu cầu.
Một cách khác để yêu cầu ảnh cho một địa điểm là đưa ra yêu cầu Place Details (New) (Thông tin chi tiết về địa điểm (Mới)), bao gồm cả GMSPlacePropertyPhotos trong danh sách trường. Lợi ích của việc thực hiện lệnh gọi Chi tiết về địa điểm là đối tượng phản hồi GMSPlace có thể chứa ảnh và mọi trường dữ liệu khác mà bạn muốn cho địa điểm.
Mã mẫu
Phương thức ví dụ sau đây lấy mã địa điểm và nhận ảnh đầu tiên trong danh sách được trả về. Bạn có thể sử dụng phương thức này làm mẫu cho phương thức mà bạn sẽ tạo trong ứng dụng của riêng mình.
Places Swift SDK
// First fetch place details// A hotel in Saigon with an attribution.letplaceID="ChIJV4k8_9UodTERU5KXbkYpSYs"letfetchPlaceRequest=FetchPlaceRequest(placeID:placeID,placeProperties:[.name,.website])varfetchedPlace:PlaceswitchawaitplacesClient.fetchPlace(with:fetchPlaceRequest){case.success(letplace):fetchedPlace=placecase.failure(letplacesError):// Handle error}// Use the place details to fetch a photo's image.guardletphoto=fetchedPlace.photos?.firstelse{// Handle place without photos.}letfetchPhotoRequest=FetchPhotoRequest(photo:photo,maxSize:CGSizeMake(4800,4800))switchawaitplacesClient.fetchPhoto(with:fetchPhotoRequest){case.success(letuiImage):// Handle image.case.failure(letplacesError):// Handle error}
Swift
// A hotel in Saigon with an attribution.letplaceID="ChIJV4k8_9UodTERU5KXbkYpSYs"// Request list of photos for a placeplacesClient.lookUpPhotos(forPlaceID:placeID){(photos,error)inguardletphotoMetadata:GMSPlacePhotoMetadata=photos?.results[0]else{return}// Request individual photos in the response listletfetchPhotoRequest=GMSFetchPhotoRequest(photoMetadata:photoMetadata,maxSize:CGSizeMake(4800,4800))self.client.fetchPhoto(with:fetchPhotoRequest,callback:{(photoImage:UIImage?,error:Error?)inguardletphotoImage,error==nilelse{print("Handle photo error: ")return}print("Display photo Image: ")})}
Objective-C
// A hotel in Saigon with an attribution.NSString*placeID=@"ChIJV4k8_9UodTERU5KXbkYpSYs";[placesClientlookUpPhotosForPlaceID:placeIDcallback:^(GMSPlacePhotoMetadataList*list,NSError*error){GMSPlacePhotoMetadata*photoMetadata=[listresults][0];// Request individual photos in the response listGMSFetchPhotoRequest*fetchPhotoRequest=[[GMSFetchPhotoRequestalloc]initWithPhotoMetadata:photoMetadatamaxSize:CGSizeMake(4800,4800)];[placesClientfetchPhotoWithRequest:fetchPhotoRequestcallback:^(UIImage*_NullablephotoImage,NSError*_Nullableerror){if(error==nil){// Display photo}}];}];
Để định cấu hình hành vi lưu vào bộ nhớ đệm, bạn có thể thay đổi bộ nhớ đệm URL dùng chung bằng cách sử dụng [NSURLCache setSharedURLCache:] trong phương thức application:didFinishLaunchingWithOptions: của uỷ quyền ứng dụng.
Nếu không muốn ứng dụng của mình chia sẻ một NSURLCache với Places SDK cho iOS, bạn có thể tạo một NSURLCache mới và chỉ sử dụng NSURLCache này trong ứng dụng mà không cần đặt làm bộ nhớ đệm dùng chung.
Phân bổ
Trong hầu hết trường hợp, bạn có thể sử dụng ảnh về địa điểm mà không cần ghi nhận quyền tác giả hoặc sẽ có thông tin ghi nhận quyền tác giả bắt buộc trong ảnh. Tuy nhiên, nếu phiên bản GMSPlacePhotoMetadata được trả về có chứa attributions hoặc authorAttribution, bạn phải thêm thông tin ghi nhận quyền tác giả này vào ứng dụng của mình bất cứ khi nào bạn hiển thị hình ảnh. Xem tài liệu về phân bổ.
[null,null,["Cập nhật lần gần đây nhất: 2025-08-31 UTC."],[[["\u003cp\u003eThe Places SDK for iOS allows you to request and display place photos, which are bitmap images up to 4800x4800 pixels, from various sources.\u003c/p\u003e\n"],["\u003cp\u003eYou can request up to 10 photos for a place using \u003ccode\u003eGMSPlacesClient\u003c/code\u003e methods like \u003ccode\u003elookUpPhotosForPlaceID\u003c/code\u003e and \u003ccode\u003efetchPhotoWithRequest\u003c/code\u003e, or by including \u003ccode\u003eGMSPlacePropertyPhotos\u003c/code\u003e in a Place Details request.\u003c/p\u003e\n"],["\u003cp\u003ePhotos retrieved through the SDK are cached by the Foundation URL loading system, and you can configure caching behavior using \u003ccode\u003eNSURLCache\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eWhile most place photos don't require attribution, if \u003ccode\u003eGMSPlacePhotoMetadata\u003c/code\u003e includes \u003ccode\u003eattributions\u003c/code\u003e or \u003ccode\u003eauthorAttribution\u003c/code\u003e, you must display them with the image.\u003c/p\u003e\n"],["\u003cp\u003eSample code in Swift and Objective-C demonstrates how to fetch and display place photos using the Places SDK for iOS.\u003c/p\u003e\n"]]],["The Places SDK for iOS (New) allows requesting place photos, sourced from business owners and users, for display in apps. To use this feature you first need to Enable the Places API (New). You can request up to ten photos for a place, using `GMSPlacesClient` to look up photos by place ID and receive a `GMSPlacePhotoMetadataList`. Each photo's `GMSPlacePhotoMetadata` is used to create a `GMSFetchPhotoRequest` to retrieve a `UIImage`. Photos can also be requested via Place Details. There are guidelines on attributions that must be followed. Loaded photos are cached using `NSURLCache`.\n"],null,["# Place Photos (New)\n\nSelect platform: [Android](/maps/documentation/places/android-sdk/place-photos \"View this page for the Android platform docs.\") [iOS](/maps/documentation/places/ios-sdk/place-photos \"View this page for the iOS platform docs.\") [Web Service](/maps/documentation/places/web-service/place-photos \"View this page for the Web Service platform docs.\")\n\n\u003cbr /\u003e\n\n**European Economic Area (EEA) developers** If your billing address is in the European Economic Area, effective on 8 July 2025, the [Google Maps Platform EEA Terms of Service](https://cloud.google.com/terms/maps-platform/eea) will apply to your use of the Services. Functionality varies by region. [Learn more](/maps/comms/eea/faq).\n\n\u003cbr /\u003e\n\n| This feature requires that you enable Places API (New), the next generation of the Places API. For more information, see [Enable APIs](/maps/documentation/places/ios-sdk/cloud-setup#enabling-apis).\n\n\u003cbr /\u003e\n\nYou can use the Places SDK for iOS (New) to request place photos to\ndisplay in your application. Photos returned by the photos service come from a\nvariety of sources, including business owners and user-contributed photos.\n\nPhotos are bitmap images represented by a\n[UIImage](https://developer.apple.com/documentation/uikit/uiimage) object.\nA bitmap image has a maximum size of 4800 by 4800 pixels.\n\nRequest an image\n----------------\n\nYou can request up to 10 photos for a place:\n\n1. Call\n [`[GMSPlacesClient lookUpPhotosForPlaceID]`](/maps/documentation/places/ios-sdk/reference/objc/Classes/GMSPlacesClient#-lookupphotosforplaceid:callback:),\n passing a place ID and a\n [`GMSPlacePhotoMetadataResultCallback`](/maps/documentation/places/ios-sdk/reference/objc/Type-Definitions#/c:GMSPlacesClient.h@T@GMSPlacePhotoMetadataResultCallback) callback.\n This request calls the `GMSPlacePhotoMetadataResultCallback` callback with a\n [`GMSPlacePhotoMetadataList`](/maps/documentation/places/ios-sdk/reference/objc/Classes/GMSPlacePhotoMetadataList)\n object.\n\n2. From the `GMSPlacePhotoMetadataList` object in the callback, the\n [`results`](/maps/documentation/places/ios-sdk/reference/objc/Classes/GMSPlacePhotoMetadataList#results)\n array property contains the photos, where each photo is represented by a\n [`GMSPlacePhotoMetadata`](/maps/documentation/places/ios-sdk/reference/objc/Classes/GMSPlacePhotoMetadata)\n object.\n\n3. Use the `GMSPlacePhotoMetadata` object to create a\n [`GMSFetchPhotoRequest`](/maps/documentation/places/ios-sdk/reference/objc/Classes/GMSFetchPhotoRequest),\n including the maximum size of the requested image.\n\n4. For each `GMSPlacePhotoMetadata` object in the array, call\n [`[GMSPlacesClient\n fetchPhotoWithRequest:callback:]`](/maps/documentation/places/ios-sdk/reference/objc/Classes/GMSPlacesClient#-fetchphotowithrequest:callback:)\n passing the `GMSFetchPhotoRequest` object. This methods calls the\n [`GMSFetchPhotoResultCallback`](/maps/documentation/places/ios-sdk/reference/objc/Type-Definitions#/c:GMSPlacesClient.h@T@GMSFetchPhotoResultCallback)\n callback with a usable bitmap image as a UIImage.\n\nAnother way to request photos for a place is to make a\n[Place Details (New)](/maps/documentation/places/ios-sdk/details-place) request,\nincluding `GMSPlacePropertyPhotos` in the field list. The advantage to making a\nPlace Details call is that the response\n[`GMSPlace`](/maps/documentation/places/ios-sdk/reference/objc/Classes/GMSPlace)\nobject can contain the photos and any other data fields that you want for\nthe place.\n| **Note:** Whenever you display a place photo in your app, make sure to follow the attribution guidelines. See [Attributions](#attributions) for more information.\n\nSample code\n-----------\n\nThe following example method takes a place ID and gets the first photo in the\nreturned list. You can use this method as a template for the method you will\ncreate in your own app. \n\n### Places Swift SDK\n\n```swift\n// First fetch place details\n// A hotel in Saigon with an attribution.\nlet placeID = \"ChIJV4k8_9UodTERU5KXbkYpSYs\"\nlet fetchPlaceRequest = FetchPlaceRequest(\n placeID: placeID,\n placeProperties: [ . name, .website ]\n)\nvar fetchedPlace: Place\nswitch await placesClient.fetchPlace(with: fetchPlaceRequest) {\ncase .success(let place):\n fetchedPlace = place\ncase .failure(let placesError):\n // Handle error\n}\n\n// Use the place details to fetch a photo's image.\nguard let photo = fetchedPlace.photos?.first else {\n // Handle place without photos.\n}\nlet fetchPhotoRequest =\n FetchPhotoRequest(photo: photo, maxSize: CGSizeMake(4800, 4800))\nswitch await placesClient.fetchPhoto(with: fetchPhotoRequest) {\ncase .success(let uiImage):\n // Handle image.\ncase .failure(let placesError):\n // Handle error\n}\n```\n\n### Swift\n\n```swift\n// A hotel in Saigon with an attribution.\nlet placeID = \"ChIJV4k8_9UodTERU5KXbkYpSYs\"\n\n// Request list of photos for a place\nplacesClient.lookUpPhotos(forPlaceID: placeID) { (photos, error) in\n\n guard let photoMetadata: GMSPlacePhotoMetadata = photos?.results[0] else {\n return }\n\n // Request individual photos in the response list\n let fetchPhotoRequest = GMSFetchPhotoRequest(photoMetadata: photoMetadata, maxSize: CGSizeMake(4800, 4800))\n self.client.fetchPhoto(with: fetchPhotoRequest, callback: {\n (photoImage: UIImage?, error: Error?) in\n guard let photoImage, error == nil else {\n print(\"Handle photo error: \")\n return }\n print(\"Display photo Image: \")\n }\n )\n}\n```\n\n### Objective-C\n\n```objective-c\n// A hotel in Saigon with an attribution.\nNSString *placeID = @\"ChIJV4k8_9UodTERU5KXbkYpSYs\";\n\n[placesClient lookUpPhotosForPlaceID:placeID callback: ^(GMSPlacePhotoMetadataList *list, NSError *error) {\n GMSPlacePhotoMetadata *photoMetadata = [list results][0];\n\n // Request individual photos in the response list\n GMSFetchPhotoRequest *fetchPhotoRequest = [[GMSFetchPhotoRequest alloc] initWithPhotoMetadata:photoMetadata maxSize:CGSizeMake(4800, 4800)];\n [placesClient fetchPhotoWithRequest:fetchPhotoRequest callback: ^(UIImage *_Nullable photoImage, NSError *_Nullable error) {\n if (error == nil) {\n // Display photo\n }\n }];\n}];\n```\n\nCaching\n-------\n\nPhotos loaded using [`[GMSPlacesClient loadPlacePhoto:callback:]`](/maps/documentation/places/ios-sdk/reference/objc/Classes/GMSPlacesClient#-loadplacephoto:callback:)\nor [`[GMSPlacesClient loadPlacePhoto:constrainedToSize:scale:callback:]`](/maps/documentation/places/ios-sdk/reference/objc/Classes/GMSPlacesClient#-loadplacephoto:constrainedtosize:scale:callback:)\nare cached both on disk and in-memory by the [Foundation URL loading system](https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/URLLoadingSystem/URLLoadingSystem.html)\nin the shared [`NSURLCache`](https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSURLCache_Class/index.html).\n\nTo configure the caching behavior you can change the shared URL cache using\n[`[NSURLCache setSharedURLCache:]`](https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSURLCache_Class/index.html#//apple_ref/occ/clm/NSURLCache/setSharedURLCache:)\nin your application delegate's `application:didFinishLaunchingWithOptions:`\nmethod.\n\nIf you do not want your application to share a `NSURLCache` with the\nPlaces SDK for iOS you can create a new `NSURLCache` and use this\nexclusively within your app without setting it as the shared cache.\n\nAttributions\n------------\n\nIn most cases, place photos can be used without attribution, or will have the\nrequired attribution included as part of the image. However, if the returned\n[`GMSPlacePhotoMetadata`](/maps/documentation/places/ios-sdk/reference/objc/Classes/GMSPlacePhotoMetadata)\ninstance includes any\n[`attributions`](/maps/documentation/places/ios-sdk/reference/objc/Classes/GMSPlacePhotoMetadata#attributions)\nor\n[`authorAttribution`](/maps/documentation/places/ios-sdk/reference/objc/Classes/GMSPlacePhotoMetadata#authorattributions),\nyou must include these attributions in your application wherever you display the\nimage. See the documentation on\n[attributions](/maps/documentation/places/ios-sdk/policies#other_attribution_requirements)."]]