Moving a MapMarker on a SwiftUI/MapKit Map View

In my app I'm refreshing data and as part of that I need my Map view and a MapMarker on that Map view to move with it. The Map view is moving as expected, but the MapMarker is remaining where it is when the view is first presented.

Code Block swift
struct MapView: View {
    @Binding var train: Train
    @Binding var region: MKCoordinateRegion
    @Binding var marker: MapMarker
    
    var body: some View {
        Map(coordinateRegion: $region, interactionModes: .zoom, annotationItems: [train], annotationContent: { (_) in
            return marker
        })
        .edgesIgnoringSafeArea(.bottom)
        .navigationBarTitle("Train #\(train.runNumber)")
    }
}
struct MapView_Previews: PreviewProvider {
    @State static var train = sampleTrain1
    @State static var region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: sampleTrain1.position!.latitude, longitude: sampleTrain1.position!.longitude), latitudinalMeters: 1000, longitudinalMeters: 1000)
    @State static var marker = MapMarker(coordinate: region.center)
    
    static var previews: some View {
        NavigationView {
            MapView(train: $train, region: $region, marker: $marker)
        }
    }
}


Seems using @Binding in annotationContent is a bad idea.
Code Block
struct MapView: View {
    @Binding var train: Train
    @Binding var region: MKCoordinateRegion
    
    var body: some View {
        Map(coordinateRegion: $region, interactionModes: .zoom, annotationItems: [train], annotationContent: {train in
            MapMarker(coordinate: train.position!)
        })
        .edgesIgnoringSafeArea(.bottom)
        .navigationBarTitle("Train #\(train.runNumber)")
    }
}


Why are you ignoring the parameter in your closure for annotationContent?
Ignoring the parameter and bypassing it was one of my failed attempts at getting it to work. Ignoring vs not ignoring the parameter seemed to do nothing, though ignoring it is irrational. The version committed to my git repository is as follows (where I chose not to ignore the parameter in the closure):

Code Block swift
@available(iOS 14.0, *)
struct MapView: View {
@Binding var train: Train
@Binding var region: MKCoordinateRegion
var body: some View {
Map(coordinateRegion: $region, interactionModes: .zoom, annotationItems: [train], annotationContent: { (train) in
return MapMarker.init(coordinate: CLLocationCoordinate2D(latitude: CLLocationDegrees(train.position!.latitude), longitude: CLLocationDegrees(train.position!.longitude)))
})
.edgesIgnoringSafeArea(.bottom)
.navigationBarTitle("Train #\(train.runNumber)")
}
}
@available(iOS 14.0, *)
struct MapView_Previews: PreviewProvider {
@State static var train = sampleTrain1
@State static var region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: sampleTrain1.position!.latitude, longitude: sampleTrain1.position!.longitude), latitudinalMeters: 1000, longitudinalMeters: 1000)
static var previews: some View {
NavigationView {
MapView(train: $train, region: $region)
}
}
}


Immediately after posting that I realized that putting in the position is much cleaner than what I just posted.

The version committed to my git repository is as follows 

Thanks for showing your code.

And with testing your actual code, I found that changing only the properties of train has no effect.
But assigning a new instance of Train (meaning id is also updated), the MapMarker moves.

I think this is sort of a bug, and you should better send a feedback to Apple.
Thanks!
Moving a MapMarker on a SwiftUI/MapKit Map View
 
 
Q