Greetings,
I'm attempting to refactor a SwiftUI application.
From: IUViewRepresentable of MapView
To: SwiftUI's native Map() view.
However my application has to be able to react to the user's panning and zooming. In my UIViewRepresentable version, I added MKMapViewDelagate protocol to the Coordinator class, and create mapView(_ mapView:regionDidChangeAnimated)
How can assign a delegate class to the SwiftUI native version to accomplish this?
I've seen some posts use an init() method to adjust the appearance of the map with MKMapView.appearance(). Turns out this has a delegate property, but assigning a delegate here does not result in the mapView:regionDidChangeAnimated method being called...
I'm attempting to refactor a SwiftUI application.
From: IUViewRepresentable of MapView
To: SwiftUI's native Map() view.
However my application has to be able to react to the user's panning and zooming. In my UIViewRepresentable version, I added MKMapViewDelagate protocol to the Coordinator class, and create mapView(_ mapView:regionDidChangeAnimated)
How can assign a delegate class to the SwiftUI native version to accomplish this?
I've seen some posts use an init() method to adjust the appearance of the map with MKMapView.appearance(). Turns out this has a delegate property, but assigning a delegate here does not result in the mapView:regionDidChangeAnimated method being called...
Map takes a Binding<MKCoordinateRegion> parameter so you can watch this for changes by adding the .onChange modifier.
Code Block Swift struct MapView: View { @State private var region = MKCoordinateRegion(...) var body: some View { Map(coordinateRegion: $region) .onChange(of: region) { newRegion in // the region changed to newRegion } } }