Puoi utilizzare Places SDK for iOS (New) per richiedere foto di luoghi da visualizzare nella tua applicazione. Le foto restituite dal servizio foto provengono da una
varietà di fonti, tra cui proprietari di attività e foto inviate dagli utenti.
Le foto sono immagini bitmap rappresentate da un oggetto
UIImage.
Un'immagine bitmap ha una dimensione massima di 4800 x 4800 pixel.
Dall'oggetto GMSPlacePhotoMetadataList nel callback, la proprietà
results
dell'array contiene le foto, dove ogni foto è rappresentata da un oggetto
GMSPlacePhotoMetadata.
Utilizza l'oggetto GMSPlacePhotoMetadata per creare un'GMSFetchPhotoRequest, inclusa la dimensione massima dell'immagine richiesta.
Un altro modo per richiedere foto per un luogo è inviare una richiesta
Place Details (New),
inclusa GMSPlacePropertyPhotos nell'elenco dei campi. Il vantaggio di effettuare una chiamata
Place Details è che l'oggetto
GMSPlace
può contenere le foto e qualsiasi altro campo di dati che ti interessa per
il luogo.
Codice di esempio
Il seguente metodo di esempio accetta un ID luogo e recupera la prima foto nell'elenco restituito. Puoi utilizzare questo metodo come modello per il metodo che creerai nella tua app.
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}}];}];
Per configurare il comportamento di memorizzazione nella cache, puoi modificare la cache degli URL condivisi utilizzando
[NSURLCache setSharedURLCache:]
nel metodo application:didFinishLaunchingWithOptions: del delegato dell'applicazione.
Se non vuoi che la tua applicazione condivida una NSURLCache con l'SDK Places per iOS, puoi creare una nuova NSURLCache e utilizzarla esclusivamente all'interno della tua app senza impostarla come cache condivisa.
Attribuzioni
Nella maggior parte dei casi, le foto dei luoghi possono essere utilizzate senza attribuzione o l'attribuzione richiesta è inclusa nell'immagine. Tuttavia, se l'istanza
GMSPlacePhotoMetadata
restituita include
attributions
o
authorAttribution,
devi includere queste attribuzioni nella tua applicazione ovunque visualizzi l'immagine. Consulta la documentazione
sulle attribuzioni.
[null,null,["Ultimo aggiornamento 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)."]]