当地图上放置太多注释时,它开始滞后。这似乎是由于缩放导致注释被一遍又一遍地渲染。有没有人有办法让这项工作更有效? CPU 也跳起来了,但它声称调试可能会弹出数百次警告,我无法修复 whiteout 使区域常量:“不允许从视图更新中发布更改,这将导致未定义的行为。 “
@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 当地图加载并调用此函数时调用,该函数将注释添加到数组:
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)
}
}
}