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

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

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

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

  1. 启用 Places API (新)
  2. Places Swift SDK for iOS(预览版) 添加到依赖项
  3. 使用以下命令初始化 Places 客户端: PlacesClient

分步迁移示例

例如,假设某个使用 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 中采用 switch/await 模式的回调 placesSwiftClient.fetchPlace

// 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)")
}