迁移到 Places Swift SDK for iOS(预览版)

从 Places SDK for iOS 迁移到 Places Swift SDK for iOS(预览版)应该很简单,并且可以逐步完成。由于适用于 iOS 的 Places Swift SDK(预览版)中的结构体与基于 Objective-C 的结构体不兼容,因此我们建议您根据 GMSPlacesClient 中的 API 用法迁移各个功能块。

将适用于 iOS 的 Places Swift SDK(预览版)添加到您的项目

如需使用适用于 iOS 的 Places Swift SDK(预览版),请按以下步骤操作:

  1. 启用 Places API(新)
  2. Places Swift SDK for iOS(预览版)添加到您的依赖项中。您可以选择安装 GooglePlaces 和/或 GooglePlacesSwift

  3. 使用 PlacesClient 初始化 Places 客户端。

分步迁移示例

例如,假设某个使用 Places SDK for iOS 的应用根据文本输入内容接收自动补全建议,然后提取第一个地点建议的详细信息。使用 Places SDK for iOS 时,现有代码可能如下所示:

// Initialize Places Client.
GMSPlacesClient.provideAPIKey(apiKey)
let client = GMSPlacesClient.shared()

// Fetch Autocomplete Request.
let center = CLLocation(latitude: 37.3913916, longitude: -122.0879074)
let northEast = CLLocationCoordinate2DMake(37.388162, -122.088137)
let southWest = CLLocationCoordinate2DMake(37.395804, -122.077023)

let filter = GMSAutocompleteFilter()
filter.types = [kGMSPlaceTypeRestaurant]
filter.origin = center
filter.locationBias = GMSPlaceRectangularLocationOption(northEast, southWest)

let request = GMSAutocompleteRequest(query: "Sicilian piz")
request.filter = filter

client.fetchAutocompleteSuggestions(from: request) { (results, error) in
  guard let results, error == nil else {
    print("Autocomplete error: \(String(describing: error))")
    return
  }

  // Fetch Place Request.
  guard let placeID = results.first?.placeSuggestion?.placeID else { return }
  let myProperties = [GMSPlaceProperty.name, GMSPlaceProperty.website].map {$0.rawValue}

  let fetchPlaceRequest = GMSFetchPlaceRequest(placeID: placeID, placeProperties: myProperties, sessionToken: nil)

  client.fetchPlace(with: fetchPlaceRequest) { (place: GMSPlace?, error: Error?) in
    guard let place, error == nil else { return }
    print("Place found: \(String(describing: place.name)); \(String(describing: place.website))")
  }
}

更新 Places Client 初始化程序

如需改进代码并利用新 SDK 的功能,您需要将 GMSPlacesClient 替换为 PlacesClient。此外,新方法中的参数名称已更改,因此您需要将参数更新为 from,而不是 with。最后,Places Swift SDK for iOS(预览版)使用升级后的 AutocompleteRequest

更新后的代码

// Initialize Places Swift Client.
let _ = PlacesClient.provideAPIKey(apiKey)
let placesSwiftClient = PlacesClient.shared

原始代码

// Initialize Places Client.
GMSPlacesClient.provideAPIKey(apiKey)
let client = GMSPlacesClient.shared()

更新自动补全请求

您可以先更新自动补全请求流程。旧代码使用回调请求自动补全建议,而新代码采用 switch/await 模式。回调可能会增加代码结构和错误处理的复杂性。新的 Places Swift SDK for iOS(预览版)支持并发,这简化了异步操作。

// Initialize Places Swift Client.
let _ = PlacesClient.provideAPIKey(apiKey)
let placesSwiftClient = PlacesClient.shared

// Fetch Autocomplete Request.
let center = CLLocation(latitude: 37.3913916, longitude: -122.0879074)
let northEast = CLLocationCoordinate2DMake(37.388162, -122.088137)
let southWest = CLLocationCoordinate2DMake(37.395804, -122.077023)

let bias = RectangularCoordinateRegion(northEast: northEast, southWest: southWest)
let filter = AutocompleteFilter(types: [ .restaurant ], origin: center, coordinateRegionBias: bias)

let autocompleteRequest = AutocompleteRequest(query: "Sicilian piz", filter: filter)
let placeID: String
switch await placesSwiftClient.fetchAutocompleteSuggestions(with: autocompleteRequest) {
case .success(let results):
  switch results.first {
  case .place(let placeSuggestion):
    placeID = placeSuggestion.placeID
  case .none:
    fallthrough
  @unknown default:
    return
  }
case .failure(let placesError):
  print("Autocomplete error: \(placesError)")
  return
}

// Initialize Places Client.
GMSPlacesClient.provideAPIKey(apiKey)
let placesClient = GMSPlacesClient.shared()

// Fetch Place Request.
let myProperties = [GMSPlaceProperty.name, GMSPlaceProperty.website].map {$0.rawValue}

let fetchPlaceRequest = GMSFetchPlaceRequest(placeID: placeID, placeProperties: myProperties, sessionToken: nil)

placesClient.fetchPlace(with: fetchPlaceRequest) { (place: GMSPlace?, error: Error?) in
  guard let place, error == nil else { return }
  print("Place found: \(String(describing: place.name)); \(String(describing: place.website))")
}

更新方法和类名称

最后,通过重构 fetchPlace 代码并移除 GMSPlacesClient 初始化和 GMSPlaceProperty 声明,完成迁移。在 Places Swift SDK for iOS(预览版)中,方法和类名称已更新为移除“GMS”前缀,并且必须相应地进行更新;例如,GMSFetchPlaceRequest 变为 FetchPlaceRequest

类型处理

新版 fetchPlace 方法使用了经过改进的类型处理功能。虽然旧代码需要传递属性的原始值,但新代码不需要开发者在此处显式提取原始值,从而提高了简洁性和可读性。

并发

此外,新方法支持并发,允许将 placesClient.fetchPlace 中的回调替换为 placesSwiftClient.fetchPlace 中的 switch/await 模式。

// Initialize Places Swift Client.
let _ = PlacesClient.provideAPIKey(apiKey)
let placesSwiftClient = PlacesClient.shared

// Fetch Autocomplete Request.
let center = CLLocation(latitude: 37.3913916, longitude: -122.0879074)
let northEast = CLLocationCoordinate2DMake(37.388162, -122.088137)
let southWest = CLLocationCoordinate2DMake(37.395804, -122.077023)

let bias = RectangularCoordinateRegion(northEast: northEast, southWest: southWest)
let filter = AutocompleteFilter(types: [ .restaurant ], origin: center, coordinateRegionBias: bias)

let autocompleteRequest = AutocompleteRequest(query: "Sicilian piz", filter: filter)
let placeID: String
switch await placesSwiftClient.fetchAutocompleteSuggestions(with: autocompleteRequest) {
case .success(let results):
  switch results.first {
  case .place(let placeSuggestion):
    placeID = placeSuggestion.placeID
  case .none:
    fallthrough
  @unknown default:
    return
  }
case .failure(let placesError):
  print("Autocomplete error: \(placesError)")
  return
}

// Fetch Place Request.
let fetchPlaceRequest = FetchPlaceRequest(placeID: placeID, placeProperties: [.displayName, .websiteURL])
switch await placesSwiftClient.fetchPlace(with: fetchPlaceRequest) {
case .success(let place):
  print("Place found: \(place.displayName): \(String(describing: place.description))")

case .failure(let placesError):
  print("Place not found: \(placeID); \(placesError)")
}

随时掌握最新动态

如需了解新功能和变更,请访问 适用于 iOS 的 Places Swift SDK(预览版)的版本说明页面