I'm hoping someone might point me in the right direction to fix an issue I'm having with a Map view. I'm using the latest XCode 15, targetting iOS 17. I have a map view where the user can tap on the map to create a new annotation marker. However after adding the onTapGesture
handler my MapUserLocationButton
isn't triggered, instead it drops a pin behind where the button is. The compass button still works which is odd.
Here's the code for the view (with a few bits removed to keep it concise)
struct LocationsMapView: View {
@Environment(\.modelContext) private var context
@Query private var locations: [SavedLocation]
@Binding var path: NavigationPath
@State private var position: MapCameraPosition = .automatic
@State private var selectedLocation: SavedLocation?
@State var placedCreateLocationPin = false
@State var createLocationCoord :CLLocationCoordinate2D? = nil
var body: some View {
GeometryReader { proxy in
MapReader{ reader in
Map(position: $position, selection: $selectedLocation){
if let pl = createLocationCoord {
if placedCreateLocationPin{
Annotation("New Location", coordinate: pl){
Image("PinIcon")
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 44, height: 44)
}
}
}
}
.mapStyle(.hybrid)
.safeAreaInset(edge: .bottom){
if placedCreateLocationPin {
Button {
if let pl = createLocationCoord{
let vm = CreateLocationViewModel(location: pl)
path.append(vm)
placedCreateLocationPin = false
}
} label: {
Text("Create Location")
}
}
}
.mapControls{
MapCompass()
MapUserLocationButton()
}
.onTapGesture(perform: { screenCoord in
placedCreateLocationPin.toggle()
let pl = reader.convert(screenCoord, from: .local)
print("tapped location: \(pl?.latitude ?? 0.0) \(pl?.longitude ?? 0.0)")
createLocationCoord = pl
})
}
}
}
}