MapKit onTapGesture breaks UserLocationButton

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
                })
            }
        }
    }
}
Post not yet marked as solved Up vote post of Underhillj Down vote post of Underhillj
959 views

Replies

Yeah I am having the same issue. Other buttons work fine, even custom buttons, but the MapUserLocationButton does not.

I raised a TSI with Apple over the month ago but I've not received any response on it.

I raised a TSI with Apple over the month ago but I've not received any response on it.

Please contact me via email. My address is in my signature. Make sure to reference this thread because, hey, I get a lot of email |-:

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Yeah, I'm having this issue too. Only on iOS, not on macOS. A long-press on the button does work, presumably because that bypasses the onTapGesture handler.

Submitted Feedback: FB13423892.

Add a Comment

Still nothing about this issue?

Same issue here. Very frustrating.

I found one workaround. Use a ZStack and manually overlay the controls rather than using .mapControls. You'll nee a mapScope for that.