user1465078
user1465078

Reputation:

Why would MKMapView not add new annotations from Firestore data?

I am using an MKMapView wrapped in UIVIewRepresentable under SwiftUI to display various locations on the map. The data is fetched from a Google Firestore collection and annotations added to the map view when the app loads - no problem there. Locations are also loaded into a lazy grid for display - also no problems. The issue arises if a new location is added to the Firestore set no corresponding annotation(s) will be added to the map view. They will be aded to the lazy grid however. Locations in Firestore can be deleted and the annotations on the map will be removed, data can be edited (like text values for display) and these will update on the map.

I'm not sure where to go from here to solve the issue ...

the updateUIView code > 'siteData' is a simple array of site structs and populated from Firestore.

func updateUIView(_ uiView: MKMapView, context: Context) {
if siteData.count != uiView.annotations.count {
    uiView.removeAnnotations(uiView.annotations)
    for points in siteData {
        let annotation = SiteAnnotation(title: points.name, subtitle: points.shortDescription, site: points, coordinate: points.locationCoordinate)
        uiView.addAnnotation(annotation)
    }
}
uiView.showsUserLocation = true
}
}

What am I missing in this?

Upvotes: 0

Views: 74

Answers (1)

user1465078
user1465078

Reputation:

Came back to this this morning and realized that the uiView.annotations.count is including the pin for the user's location marked on the map. So when if siteData.count != uiView.annotations.count {, the siteData.count and uiView.annotations.count are actually equal.

Guess it's the little things that can really trip us up.

Upvotes: 0

Related Questions