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

Replies

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,
        ...
  • Actually this doesn't work :( I'm still getting the warnings, just not immediately when I pan the map. 🤷‍♂️

  • Also, the warning goes away with no code changes in Xcode 15.0 beta 15A5160n and iOS 17.0, which again isn't a lot of help right now.

Add a Comment

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