In the view described in the following code, I would like to be able to select a marker, no matter if the marker is a Place or an Accommodation. Both Place and Accommodation are defined as classes with different fields. Both of them have latitude, and longitude. But I can only make one of them selectable depending on which one I put on this line
Map(position: $cameraPosition, selection: $selectedAccommodation)
or
Map(position: $cameraPosition, selection: $selectedAccommodation)
import SwiftUI
import MapKit
struct DestinationMapView: View {
var destination: Destination
@State private var cameraPosition: MapCameraPosition = .automatic
@State private var selectedPlace: Place?
@State private var selectedAccommodation: Accommodation?
//@State private var selectedPlace: SelectablePlace?
var body: some View {
Map(position: $cameraPosition, selection: $selectedPlace) {
ForEach(destination.accommodations ?? []) { accommodation in
Group {
if let lat = accommodation.latitude, let lon = accommodation.longitude {
Marker(accommodation.name, systemImage: "bed.double.fill", coordinate: CLLocationCoordinate2D(latitude: lat, longitude: lon))
.tint(.blue)
}
}
.tag(accommodation)
}
ForEach(destination.places ?? []) { place in
Group {
if let lat = place.latitude, let lon = place.longitude {
Marker(place.name, systemImage: place.placeType.systemImage, coordinate: CLLocationCoordinate2D(latitude: lat, longitude: lon))
}
}
.tag(place)
}
}
.sheet(item: $selectedPlace) { selectedPlace in
Text("Selected Place")
.presentationDetents([.medium, .large])
.presentationDragIndicator(.visible)
}
.sheet(item: $selectedAccommodation) { selectedAccommodation in
Text("Selected Accommodation")
.presentationDetents([.medium, .large])
.presentationDragIndicator(.visible)
}
.onAppear {
if let coordinateRegion = destination.coordinateRegion {
cameraPosition = MapCameraPosition.region(coordinateRegion)
}
}
}
}
How can add the capability to select the marker no matter if it’s of Place type or Accommodation type?