TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

SwiftUI: Updating coordinates of an annotation in Map does not work

Forums > SwiftUI

I am trying to update the coordinates of an annotation in SwiftUI's Map view, but it seems that this does work if the ID of the annotation does not change.

Please see the below code. The first two functions will not change the location of the annotation on the map. The third one does. I expect this is a limitation of SwiftUI, but wanted to check if I overlooked something.

struct Annotation: Identifiable {
    let id: Int
    var coordinate: CLLocationCoordinate2D
}

let sanFrancisco = CLLocationCoordinate2D(latitude: 37.773972, longitude: -122.431297)

struct MapAnimation: View {
    @State private var coordinateRegion = MKCoordinateRegion(center: sanFrancisco, latitudinalMeters: 10000, longitudinalMeters: 10000)

    @State private var annotations = [
        Annotation(id: 1, coordinate: sanFrancisco)
    ]

    var body: some View {
        ZStack(alignment: .bottom) {
            Map(coordinateRegion: $coordinateRegion, annotationItems: annotations) { annotation in
                MapMarker(coordinate: annotation.coordinate)
            }

            Button(action: moveAnnotation1, label: {
                Text("Move Annotation")
                    .padding(4)
                    .background(Color.blue)
                    .cornerRadius(3.0)
                    .padding()
                    .foregroundColor(.white)
            })
        }
    }

    // Changing the coordinate of the annotation in place does not work
    private func moveAnnotation1() {
        let newCoordinates = CLLocationCoordinate2D(latitude: sanFrancisco.latitude + 0.05, longitude: sanFrancisco.longitude)
        annotations[0].coordinate = newCoordinates
    }

    // Replacing the entire array with a new Annotation object does not work.
    // if the id of the new annotation is the same as the old annotation (in this case id: 1)
    private func moveAnnotation2() {
        let newCoordinates = CLLocationCoordinate2D(latitude: sanFrancisco.latitude + 0.05, longitude: sanFrancisco.longitude)
        let newAnnotation = Annotation(id: 1, coordinate: newCoordinates)
        annotations = [newAnnotation]
    }

    // This one works, because create a new annotation object with a new id
    private func moveAnnotation3() {
        let newCoordinates = CLLocationCoordinate2D(latitude: sanFrancisco.latitude + 0.05, longitude: sanFrancisco.longitude)
        let newAnnotation = Annotation(id: 2, coordinate: newCoordinates)
        annotations = [newAnnotation]
    }

}

2      

Hacking with Swift is sponsored by String Catalog.

SPONSORED Get accurate app localizations in minutes using AI. Choose your languages & receive translations for 40+ markets!

Localize My App

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.