Skip to content

Can't animate moving MapViewAnnotation #2213

Open
@feelingsonice

Description

@feelingsonice

Observed behavior and steps to reproduce

Changing the coordinate of a MapViewAnnotation via withAnimation causes the view to re-appear instead of moving to the new location.

import SwiftUI
@_spi(Experimental) import MapboxMaps

struct MapboxMapView: View {
    private let position = Viewport.camera(
        center: .londonA,
        zoom: 16
    )
    @State var curLocation: CLLocationCoordinate2D = .londonA

    var body: some View {
        Map(initialViewport: position) {
            MapViewAnnotation(coordinate: curLocation) {
                Circle()
            }
        }
        .ignoresSafeArea()
        .overlay(alignment: . bottomTrailing) {
            Button("Change location") {
                withAnimation {
                    curLocation = .londonB // SHOULD CAUSE THE ANNOTATION TO MOVE, NOT RE-APPEAR
                }
            }
        }
    }
}

extension CLLocationCoordinate2D {
    static let londonA = CLLocationCoordinate2D(
        latitude: 51.507222,
        longitude: -0.1275
    )
    
    static let londonB = CLLocationCoordinate2D(
        latitude: 51.508222,
        longitude: -0.1276
    )
}

Expected behavior

The annotation should move to the new location.

Notes / preliminary analysis

Seems to have been possible in UIKit examples but the behavior seems different in SwiftUI.

Metadata

Metadata

Assignees

No one assigned

    Labels

    SwiftUIIssues and features related to SwiftUI support.feature 🍏When working on a new feature or feature enhancement

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions