从 Places SDK for iOS 迁移到 Places Swift SDK for iOS 应该非常简单,并且可以逐步完成。由于适用于 iOS 的 Places Swift SDK 中的结构体与其基于 Objective-C 的对应项不兼容,因此我们建议根据 GMSPlacesClient 中 API 的使用情况迁移离散的功能块。
[null,null,["最后更新时间 (UTC):2025-08-31。"],[],[],null,["# Migrate to Places Swift SDK for iOS\n\nMigrating from Places SDK for iOS to\nPlaces Swift SDK for iOS should be straightforward and can be done\nincrementally. Since structs in the Places Swift SDK for iOS are not\ncompatible with their Objective-C based counterparts, we recommend migrating\ndiscrete chunks of functionality based on uses of APIs in GMSPlacesClient.\n\nAdd the Places Swift SDK for iOS to your project\n------------------------------------------------\n\nThe following steps are required to use Places Swift SDK for iOS:\n\n1. Enable the [Places API\n (New)](/maps/documentation/places/ios-sdk/cloud-setup#enabling-apis).\n2. Add the [Places Swift SDK](/maps/documentation/places/ios-sdk/config#googleplacesswift)\n to your dependencies. You can choose to install `GooglePlaces`,\n `GooglePlacesSwift`, or both.\n\n | **Note:** The GitHub URL to access Google Places Swift has changed. If you are updating a version of GooglePlacesSwift that was accessed through the old URL, https://github.com/googlemaps/ios-places-swift-sdk, remove it from your Xcode's package dependencies section.\n3. Initialize the Places client with\n [`PlacesClient`](/maps/documentation/places/ios-sdk/config#googleplacesswift_2).\n\nStep-by-step migration example\n------------------------------\n\nAs an example, suppose an app using the Places SDK for iOS\nreceives autocomplete suggestions based on a text input, then fetches the\ndetails of the first place suggestion. Using\nPlaces SDK for iOS, the existing code might look something\nlike the following: \n\n // Initialize Places Client.\n GMSPlacesClient.provideAPIKey(apiKey)\n let client = GMSPlacesClient.shared()\n\n // Fetch Autocomplete Request.\n let center = CLLocation(latitude: 37.3913916, longitude: -122.0879074)\n let northEast = CLLocationCoordinate2DMake(37.388162, -122.088137)\n let southWest = CLLocationCoordinate2DMake(37.395804, -122.077023)\n\n let filter = GMSAutocompleteFilter()\n filter.types = [kGMSPlaceTypeRestaurant]\n filter.origin = center\n filter.locationBias = GMSPlaceRectangularLocationOption(northEast, southWest)\n\n let request = GMSAutocompleteRequest(query: \"Sicilian piz\")\n request.filter = filter\n\n client.fetchAutocompleteSuggestions(from: request) { (results, error) in\n guard let results, error == nil else {\n print(\"Autocomplete error: \\\\(String(describing: error))\")\n return\n }\n // Fetch Place Request.\n guard let placeID = results.first?.placeSuggestion?.placeID else { return }\n let myProperties = \\[GMSPlaceProperty.name, GMSPlaceProperty.website\\].map {$0.rawValue}\n let fetchPlaceRequest = GMSFetchPlaceRequest(placeID: placeID, placeProperties: myProperties, sessionToken: nil)\n client.fetchPlace(with: fetchPlaceRequest) { (place: GMSPlace?, error: Error?) in\n guard let place, error == nil else { return }\n print(\"Place found: \\\\(String(describing: place.name)); \\\\(String(describing: place.website))\")\n }\n }\n\nUpdate the Places Client initializer\n------------------------------------\n\nTo modernize the code and take advantage of the new SDK's capabilities, you'll\nneed to replace the GMSPlacesClient with the PlacesClient. Additionally, the\nparameter names are changed in the new method, so you'll need to update the\nparameter to `from` instead of `with`. Finally, the\nPlaces Swift SDK uses the upgraded\n[AutocompleteRequest](/maps/documentation/places/ios-sdk/reference/swift/Structs/AutocompleteRequest). \n\n### Updated code\n\n // Initialize Places Swift Client.\n let _ = PlacesClient.provideAPIKey(apiKey)\n let placesSwiftClient = PlacesClient.shared\n\n### Original code\n\n // Initialize Places Client.\n GMSPlacesClient.provideAPIKey(apiKey)\n let client = GMSPlacesClient.shared()\n\nUpdate the autocomplete request\n-------------------------------\n\nYou could begin by updating the autocomplete request flow. The old code uses a\ncallback to request autocomplete suggestions, while the new code employs a\n`switch`/`await` pattern. Callbacks can add complexity to code structure and\nerror handling. The new Places Swift SDK supports concurrency,\nwhich simplifies asynchronous operations. \n\n // Initialize Places Swift Client.\n let _ = PlacesClient.provideAPIKey(apiKey)\n let placesSwiftClient = PlacesClient.shared\n\n // Fetch Autocomplete Request.\n let center = CLLocation(latitude: 37.3913916, longitude: -122.0879074)\n let northEast = CLLocationCoordinate2DMake(37.388162, -122.088137)\n let southWest = CLLocationCoordinate2DMake(37.395804, -122.077023)\n\n let bias = RectangularCoordinateRegion(northEast: northEast, southWest: southWest)\n let filter = AutocompleteFilter(types: [ .restaurant ], origin: center, coordinateRegionBias: bias)\n\n let autocompleteRequest = AutocompleteRequest(query: \"Sicilian piz\", filter: filter)\n let placeID: String\n switch await placesSwiftClient.fetchAutocompleteSuggestions(with: autocompleteRequest) {\n case .success(let results):\n switch results.first {\n case .place(let placeSuggestion):\n placeID = placeSuggestion.placeID\n case .none:\n fallthrough\n @unknown default:\n return\n }\n case .failure(let placesError):\n print(\"Autocomplete error: \\\\(placesError)\")\n return\n }\n\n // Initialize Places Client.\n GMSPlacesClient.provideAPIKey(apiKey)\n let placesClient = GMSPlacesClient.shared()\n\n // Fetch Place Request.\n let myProperties = [GMSPlaceProperty.name, GMSPlaceProperty.website].map {$0.rawValue}\n\n let fetchPlaceRequest = GMSFetchPlaceRequest(placeID: placeID, placeProperties: myProperties, sessionToken: nil)\n\n placesClient.fetchPlace(with: fetchPlaceRequest) { (place: GMSPlace?, error: Error?) in\n guard let place, error == nil else { return }\n print(\"Place found: \\(String(describing: place.name)); \\(String(describing: place.website))\")\n }\n\nUpdate method and class names\n-----------------------------\n\nFinally, complete the migration by refactoring the `fetchPlace` code and\nremoving both the `GMSPlacesClient` initialization and `GMSPlaceProperty`\ndeclaration. In Places Swift SDK, the method and classnames\nhave been updated to remove the \"GMS\" prefix, and must be updated accordingly;\ne.g., `GMSFetchPlaceRequest` becomes `FetchPlaceRequest`.\n\n### Type-handling\n\nThe new `fetchPlace` method uses improved type handling. While the old code\nrequired passing the property's raw values, the new code doesn't require\ndevelopers to explicitly fetch raw values here, thereby improving concision and\nreadability.\n\n### Concurrency\n\nAdditionally, the new method supports concurrency, allowing for replacing the\ncallback in `placesClient.fetchPlace` with a `switch`/`await` pattern in\n`placesSwiftClient.fetchPlace`. \n\n // Initialize Places Swift Client.\n let _ = PlacesClient.provideAPIKey(apiKey)\n let placesSwiftClient = PlacesClient.shared\n\n // Fetch Autocomplete Request.\n let center = CLLocation(latitude: 37.3913916, longitude: -122.0879074)\n let northEast = CLLocationCoordinate2DMake(37.388162, -122.088137)\n let southWest = CLLocationCoordinate2DMake(37.395804, -122.077023)\n\n let bias = RectangularCoordinateRegion(northEast: northEast, southWest: southWest)\n let filter = AutocompleteFilter(types: [ .restaurant ], origin: center, coordinateRegionBias: bias)\n\n let autocompleteRequest = AutocompleteRequest(query: \"Sicilian piz\", filter: filter)\n let placeID: String\n switch await placesSwiftClient.fetchAutocompleteSuggestions(with: autocompleteRequest) {\n case .success(let results):\n switch results.first {\n case .place(let placeSuggestion):\n placeID = placeSuggestion.placeID\n case .none:\n fallthrough\n @unknown default:\n return\n }\n case .failure(let placesError):\n print(\"Autocomplete error: \\(placesError)\")\n return\n }\n\n // Fetch Place Request.\n let fetchPlaceRequest = FetchPlaceRequest(placeID: placeID, placeProperties: \\[.displayName, .websiteURL\\])\n switch await placesSwiftClient.fetchPlace(with: fetchPlaceRequest) {\n case .success(let place):\n print(\"Place found: \\\\(place.displayName): \\\\(String(describing: place.description))\")\n case .failure(let placesError):\n print(\"Place not found: \\\\(placeID); \\\\(placesError)\")\n }\n\nStay up-to-date\n---------------\n\nVisit the [release notes page for Places Swift SDK for iOS](/maps/documentation/places/ios-sdk/places-swift-release-notes) to learn\nabout new features and changes."]]