I have a Map in in my view as following:
and I have a ObservableObject in this view struct
MapsVM has a property:
which was used as annotation item. And its first element is used to be the center of coordinateRegion.
Map Coordinate is as following:
The weird thing happens. When I set the coordinates in mapsVM, which will publish and redraw the view.
The map region gets updated but the annotation never gets added:
is called again, but its trailing closure
was never invoked again. This causes the map only updates the region but never adds the annotation. Then I found a solution: if I set the id of MapCoordinate to UUID:
The annotation gets updated correctly now (each coordinates array assignment replaces it's first element with a new MapCoordinate, hence each assignment result in new UUID). Is this a bug? If the annotationItem's id remian the same, the Map view simply does not update the annotations.
Code Block swift Map(coordinateRegion: coordinateRegion, annotationItems: mapsVM.coordinates) { coordinate in MapAnnotation(coordinate: coordinate.coordinate, anchorPoint: CGPoint(x: 0.5, y: 0.5)) { Circle() .strokeBorder(Color.red, lineWidth: 10) .frame(width: 20, height: 20) } }
and I have a ObservableObject in this view struct
Code Block swift @ObservedObject var mapsVM: MapsVM
MapsVM has a property:
Code Block swift @Published var coordinates: [MapCoordinate]
which was used as annotation item. And its first element is used to be the center of coordinateRegion.
Map Coordinate is as following:
Code Block swift struct MapCoordinate: Identifiable { let id: Int let latitude: Double let longitude: Double var coordinate: CLLocationCoordinate2D { CLLocationCoordinate2D(latitude: latitude, longitude: longitude) } }
The weird thing happens. When I set the coordinates in mapsVM, which will publish and redraw the view.
Code Block swift coordinates[0] = MapCoordinate(id: 0, latitude: newLatitude, longitude: newLongitude)
The map region gets updated but the annotation never gets added:
Code Block swift Map(coordinateRegion: coordinateRegion, annotationItems: mapsVM.coordinates)
is called again, but its trailing closure
Code Block swift { coordinate in MapAnnotation(coordinate: coordinate.coordinate, anchorPoint: CGPoint(x: 0.5, y: 0.5)) { Circle() .strokeBorder(Color.blue, lineWidth: 10) .frame(width: 20, height: 20) } }
was never invoked again. This causes the map only updates the region but never adds the annotation. Then I found a solution: if I set the id of MapCoordinate to UUID:
Code Block swift struct MapCoordinate: Identifiable { let id = UUID() let latitude: Double let longitude: Double var coordinate: CLLocationCoordinate2D { CLLocationCoordinate2D(latitude: latitude, longitude: longitude) } }
The annotation gets updated correctly now (each coordinates array assignment replaces it's first element with a new MapCoordinate, hence each assignment result in new UUID). Is this a bug? If the annotationItem's id remian the same, the Map view simply does not update the annotations.