進階地點清單元件

您可以使用「進階地點清單」元件定義及顯示地點清單。您最多可以列出 20 個地點,這些地點必須來自 Place Details 元件支援的同一組 ID (地點 ID、地點資源名稱或經緯度座標)。

你可以選擇水平或垂直顯示清單。預設方向為直向,每個清單項目都提供與精簡和完整地點詳細資料資訊卡相同的資訊和設定選項。

使用 AdvancedPlaceListVieworientationplaceIdentifiersconfiguration 參數建立清單。詳情請參閱「範例」。

帳單

系統會針對每項清單要求收費,清單的任何變更都屬於新的計費事件。

範例

這個範例會根據地點 ID 建立清單。configuration 參數會指定每個清單項目將列出地址、評分和類型,以及其他視覺規格。系統會建立主要動作,讓使用者能將每個清單項目加入或移除我的最愛。每個清單項目的角落動作都會停用。

Swift

  struct AdvancedPlaceListDemoView: View {
  private let configuration = AdvancedPlaceListConfiguration(
    content: [.address(), .rating(), .type()],
    preferTruncation: true,
    theme: PlacesMaterialTheme(),
    attributionPosition: .bottom,
    mediaSize = .medium,
    selectable: true
  )
  @State private var placeIdentifiers = [.placeID("ChIJj61dQgK6j4AR4GeTYWZsKWw")]

  @State private var favoritePlaces: Set<String> = []

  var body: some View {
    AdvancedPlaceListView(
      orientation: .horizontal, // default is vertical
      placeIdentifiers: placeIdentifiers,
      configuration: configuration
    )
    .onLoad { results in
      for result in results {
        switch result {
          case .success(let place)
            print("place: \(place)")
          case .failure(let error)
            print("error: \(error)")
        }
      }
    }
    .onPlaceSelected { place, index in
      print("place: \(place)")
      print("index: \(index)")
    }
    .mainActions { place in
      var actions: [MainPlaceActionElement] = []
      let placeID = place.placeID!
      if self.favoritePlaces.contains(placeID) {
        actions.append(
          .action(
            MainPlaceAction(
              image: Image(systemName: "heart.fill"),
              label: "Unfavorite"
            ) { _ in self.toggleFavorite(placeID) }
          )
        )
      } else {
        actions.append(
          .action(
            MainPlaceAction(
              image: Image(systemName: "heart"),
              label: "Favorite"
            ) { _ in self.toggleFavorite(placeID) }
          )
        )
      }
      actions.append(.actionId(.openMap(style: .primary)))
      return actions
    }
    // Ensures that there are no corner actions.
    .cornerActions { _ in
      return []
    }
  }

  func toggleFavorite(_ placeID: String) {
    if favoritePlaces.contains(placeID) {
      favoritePlaces.remove(placeID)
    } else {
      favoritePlaces.insert(placeID)
    }
  }
}