Skip to content

ViewMapAnnotation not updating location #23

@dvdmrtnz

Description

@dvdmrtnz

Using MapAnnotation I can change the coordinate of an item and it changes on the map. However if I use ViewMapAnnotation, it doesn't change the location.

Here is a code example. Using MapAnnotation you can see the point move around. If you change it to ViewMapAnnotation, the point doesn't move at all.

import SwiftUI
import MapKit
import Map

struct ContentView: View {
    
    @State private var map = MKCoordinateRegion(
        center: CLLocationCoordinate2D(latitude: 40.4, longitude: -3.7),
        span: MKCoordinateSpan(latitudeDelta: 2, longitudeDelta: 2)
    )
    
    @State var locations = [
        MyLocation(coordinate:CLLocationCoordinate2D(latitude:40.4, longitude: -3.7))]
    
    var body: some View {
        Map(
            coordinateRegion: $map,
            annotationItems: locations,
            annotationContent: { location in
                MapAnnotation(coordinate: location.coordinate) { // Change MapAnnotation to ViewMapAnnotation here
                    Circle()
                    .frame(width: 25, height: 25)
                    .foregroundColor(.red)
                }
            }
        )
        .task({
            while(true)
            {
                try? await Task.sleep(nanoseconds: 1_000_000_000)
                locations[0].coordinate = CLLocationCoordinate2D(
                    latitude: 40.4 + Double.random(in: -0.5...0.5),
                    longitude: -3.7 + Double.random(in: -0.5...0.5))
            }
        })
    }
        
}

struct MyLocation : Identifiable {
    let id = UUID()
    var coordinate: CLLocationCoordinate2D
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions