When too many annotations are placed on the map it starts to lag. It seems to be due to zooming which makes be think the annotations are rendering over and over. Does anyone have a work around to have this work more efficiently? The CPU also jumps up, but it claims debugging with could be from the hundreds of times this warning pops up that I can't fix whiteout making region constant:
Publishing changes from within view updates is not allowed, this will cause undefined behavior.
@State var selected : Annotation?
@State var annotations = [Annotation(coordinates:CLLocationCoordinate2D(latitude: 39.86999190, longitude: -86.04429416), event: false)]
@State private var isPreviewing = false
var body: some View {
NavigationStack{
ZStack(alignment: .bottom) {
Map(coordinateRegion: $locationManager.region,
showsUserLocation: true,
annotationItems: annotations) { annotation in
MapAnnotation(coordinate: annotation.coordinates){
//annotations are displayed as buttons
}
}
}
}
}
on appear is called when the map loads and calls this function which adds annotations to the array:
func fetchEvents(){
var newAnnotations1 = self.annotations
let publicDB = CKContainer.default().publicCloudDatabase
let date = NSDate(timeInterval: -7.9 * 3600, since: NSDate() as Date)
let predicate = NSPredicate(format: "creationDate > %@", date)
let query = CKQuery(recordType: "event", predicate:predicate)
publicDB.fetch(withQuery: query, inZoneWith: nil) { result in
switch result {
case .success((let matchResults, _)):
for matchResult in matchResults {
switch matchResult.1 {
case .success(let record):
if let location = record["location1"] as? CLLocation {
let identified = record.recordID
newAnnotations1.append(Annotation(coordinates: location.coordinate,event:true,identify: identified))
self.annotations = newAnnotations1
}
case .failure(let error): print(error)
}
}
case .failure(let error): print(error)
}
}
}