Ask Your Question
2

How can the MKMapView userTrackingMode be reset when using SwiftUI?

asked 2023-06-12 06:02:14 +0000

devzero gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2023-06-12 06:32:02 +0000

woof gravatar image

One way to reset the MKMapView userTrackingMode when using SwiftUI is by creating a custom UIViewRepresentable for the MKMapView and implementing the updateUIView method:

struct MapView: UIViewRepresentable {
    var mapView = MKMapView()

    func makeUIView(context: Context) -> MKMapView {
        mapView.showsUserLocation = true
        return mapView
    }

    func updateUIView(_ uiView: MKMapView, context: Context) {
        if uiView.userTrackingMode != .none {
            uiView.setUserTrackingMode(.none, animated: true)
        }
    }
}

In this example, the updateUIView method checks if the current userTrackingMode is not .none, and if so, it sets it to .none to reset the user's tracking mode. The animated parameter can be set to false if you don't want the mode change to be animated.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-06-12 06:02:14 +0000

Seen: 24 times

Last updated: Jun 12 '23