ロケーションアクセスのモデルと、マップを含む SwiftUI ビューを作成します。このビューはロケーションアクセスをリクエストし、ユーザーから半径 2 km 以内のフィットネスセンターを検索して、それぞれにマーカーを表示します。
// One location manager for the resolver and the map view@MainActorfinal class LocationAccess: NSObject, ObservableObject, CLLocationManagerDelegate { @Published private(set) var status: CLAuthorizationStatus = .notDetermined var isMapOnScreen = false private let manager = CLLocationManager() var isDenied: Bool { status == .denied || status == .restricted } override init() { super.init() manager.delegate = self status = manager.authorizationStatus } // Shows the prompt only if the user hasn't answered it yet func requestAccess() { manager.requestWhenInUseAuthorization() } nonisolated func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) { let status = manager.authorizationStatus Task { @MainActor in self.status = status } }}@available(iOS 17.0, *)struct NearbyFitnessCentersMap: View { @ObservedObject var access: LocationAccess @State private var position: MapCameraPosition = .userLocation(fallback: .automatic) @State private var places: [MKMapItem] = [] var body: some View { ZStack { if access.isDenied { ContentUnavailableView( "Location access is off", systemImage: "location.slash", description: Text("To see fitness centers near you, allow location access in Settings.") ) } else { Map(position: $position) { UserAnnotation() ForEach(places, id: \.self) { place in Marker(item: place) } } .task { access.requestAccess() do { for try await update in CLLocationUpdate.liveUpdates() { guard let location = update.location else { continue } places = try await fitnessCenters(around: location.coordinate) withAnimation { position = .automatic // zoom to fit the user and the places } break } } catch { // the map still shows the location of the user } } } } // Tells the resolver that the map is on the screen .onAppear { access.isMapOnScreen = true } .onDisappear { access.isMapOnScreen = false } } private func fitnessCenters(around center: CLLocationCoordinate2D) async throws -> [MKMapItem] { let request = MKLocalPointsOfInterestRequest(center: center, radius: 2_000) // meters request.pointOfInterestFilter = MKPointOfInterestFilter(including: [.fitnessCenter]) return try await MKLocalSearch(request: request).start().mapItems }}
アプリ全体のリゾルバーに、LocationAccess モデルのプロパティを追加します。次に、map タイプと fitness_centers ID に対してマップビューを返すケースを追加します。ハイライトされた行が追加箇所です。アプリにまだアプリ全体のリゾルバーがない場合は、コード全体をコピーしてください:
struct CustomElementsResolver: AdaptyUICustomElementsResolver { let locationAccess = LocationAccess() func view( forType type: String, id: String, context: AdaptyUICustomElementContext ) -> AnyView? { switch type { case "map" where id == "fitness_centers": guard #available(iOS 17.0, *) else { return nil // iOS 16: show the fallback } if locationAccess.isDenied, !locationAccess.isMapOnScreen { return nil // access denied before the screen opened: show the fallback } return AnyView(NearbyFitnessCentersMap(access: locationAccess)) // the other cases of your resolver default: return nil } }}
// keep this instance: a resolver for one flow can reuse it@MainActor let customElementsResolver = CustomElementsResolver()AdaptyUI.setCustomElementsResolver(customElementsResolver)