SwiftUI MapKit - MapAnnotation - Publishing changes from within view updates is not allowed, this will cause undefined behavior.

So I'm trying to use MapKit in a SwiftUI project targeting iOS/iPadOS. MapKit is obviously pretty limited in SwiftUI, but I've been getting warnings trying to set up basic annotations for the user to interact with.

When I use a basic MapMarker everything is fine (although it's hard to do anything with it), but whenever I do anything with MapAnnotation, I get this warning in Xcode (14.0.1) whenever I move the map around:

[SwiftUI] Publishing changes from within view updates is not allowed, this will cause undefined behavior.

I'm no SwiftUI expert, and I get how to fix this issue when binding in something like a sheet, but I don't see how what I'm doing with MapAnnotation should be causing this.

It looks like a bug to me, possibly complaining about the $region binding, but maybe I'm wrong? Am I doing something wrong or is this a bug?

Below is some sample code that reproduces this easily for me (just launch an app with the below code and then drag the map around to see the constant warnings in Xcode). It's mostly an example from here: https://www.hackingwithswift.com/books/ios-swiftui/integrating-mapkit-with-swiftui

import SwiftUI
import MapKit

struct Location: Identifiable {
    let id = UUID()
    let name: String
    let coordinate: CLLocationCoordinate2D
}

struct ContentView: View {
    @State private var region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 51.5, longitude: -0.12), span: MKCoordinateSpan(latitudeDelta: 0.2, longitudeDelta: 0.2))

    let locations = [
        Location(name: "Buckingham Palace", coordinate: CLLocationCoordinate2D(latitude: 51.501, longitude: -0.141)),
        Location(name: "Tower of London", coordinate: CLLocationCoordinate2D(latitude: 51.508, longitude: -0.076))
    ]

    var body: some View {
        Map(coordinateRegion: $region, annotationItems: locations) { location in
            MapAnnotation(coordinate: location.coordinate) {
                Circle()
                    .stroke(.red, lineWidth: 3)
                    .frame(width: 44, height: 44)
            }
        }
        .navigationTitle("Map")
        .edgesIgnoringSafeArea(.all)
    }
}

I'm still facing this issue, are the purple warnings something I should be concerned about?

I'm experiencing the same problem, guys. I doubt it can be used in production with an issue like this.

Having this problem as well (Xcode 14.3 iOS 16.4). The problem only happens with MapAnnotation. Using MapMarker causes the purple warnings to go away.

Another option to suppress the warnings is to use a constant value for the map's coordinateRegion binding. Not possible if you actually need the value but thought I'd post it here anyways.

E.g., Use

    Map(
        coordinateRegion: .constant(mapRegion), // <-- use .constant(mapRegion) instead of $mapRegion
        annotationItems: items,
        ...

Has there been any other solution for this?

I’m on XCode 15 iOS 17 and still getting the issue with my map and custom annotations

XCode 16.0, iOS 18 and still experiencing the same issue. Confirmed being the Annotation the problem

SwiftUI MapKit - MapAnnotation - Publishing changes from within view updates is not allowed, this will cause undefined behavior.
 
 
Q