Hey Guys,
i want to create a MapView in SwiftUI that gets a list of markers and displays them. But I also want the user to be able to click/tap on them. If they get tapped they should flip a bool to mark them as done.
I've created a mockup array for my markers:
struct Marker: Identifiable {
let id = UUID()
var coordinate : CLLocationCoordinate2D
var done : Bool
}
let markers = [
Marker(coordinate: CLLocationCoordinate2D(latitude: 51.882975, longitude: 10.558585), done: true),
Marker(coordinate: CLLocationCoordinate2D(latitude: 51.883329, longitude: 10.559225), done: true),
Marker(coordinate: CLLocationCoordinate2D(latitude: 51.883296, longitude: 10.558216), done: false),
Marker(coordinate: CLLocationCoordinate2D(latitude: 51.882614, longitude: 10.559574), done: true),
Marker(coordinate: CLLocationCoordinate2D(latitude: 51.882425, longitude: 10.559263), done: false),
Marker(coordinate: CLLocationCoordinate2D(latitude: 51.882607, longitude: 10.557679), done: false),
Marker(coordinate: CLLocationCoordinate2D(latitude: 51.883246, longitude: 10.557652), done: false),
Marker(coordinate: CLLocationCoordinate2D(latitude: 51.883753, longitude: 10.557711), done: false),
Marker(coordinate: CLLocationCoordinate2D(latitude: 51.884018, longitude: 10.558710), done: false),
Marker(coordinate: CLLocationCoordinate2D(latitude: 51.881783, longitude: 10.559713), done: false),
]
The problem is that nothing is happening when I tap on the markers. I've tried to print, toggle a bool, navigate etc. in .onTapGesture { }
Here is the code for my Map:
Map(coordinateRegion: $region, annotationItems: markers) { marker in
MapAnnotation(coordinate: marker.coordinate) {
Circle()
.onTapGesture {
// do something
}
}
}
$region
is this:
@State private var region = MKCoordinateRegion(
center: CLLocationCoordinate2D(latitude: 51.882879, longitude: 10.558918),
span: MKCoordinateSpan(
latitudeDelta: 0.01,
longitudeDelta: 0.01
)
)
This is how the markers look in the simulator (I've also tried to make them bigger so there is no way to not hit them with a tap)
Hope someone can help!