我在使用@Fetchrequest属性 Package 器和将所有数据组合到一个数组时遇到了问题。特别需要,因为Mapkit API只接受一个数组作为它的注解(annotationItems:)
我有以下数组:
@FetchRequest(fetchRequest: OrgEntity.all()) private var orgs
@FetchRequest(fetchRequest: PersonEntity.all()) private var people
@FetchRequest(fetchRequest: TaskEntity.all()) private var tasks
我想为这三个数组中的每个对象显示注解,但我无法找到实现它的方法。仅使用org属性作为示例可以很好地工作,但是我如何列出所有三个属性呢?
Map(coordinateRegion: $vm.region, showsUserLocation: true, annotationItems: orgs, annotationContent: { org in
MapAnnotation(coordinate: CLLocationCoordinate2D(latitude: org.lat, longitude: org.long)) {
MapAnnotationPin()
.scaleEffect()
}
})
这是我的完整代码
import SwiftUI
import MapKit
import CoreData
struct MapView: View {
@Environment(\.dismiss) private var dismiss
@EnvironmentObject private var vm: MapViewModel
@FetchRequest(fetchRequest: OrgEntity.all()) private var orgs
@FetchRequest(fetchRequest: PersonEntity.all()) private var people
@FetchRequest(fetchRequest: TaskEntity.all()) private var tasks
var body: some View {
ZStack {
// Map(coordinateRegion: $vm.region, showsUserLocation: true)
Map(coordinateRegion: $vm.region, showsUserLocation: true, annotationItems: orgs, annotationContent: { org in
MapAnnotation(coordinate: CLLocationCoordinate2D(latitude: org.lat, longitude: org.long)) {
MapAnnotationPin()
.scaleEffect()
}
})
.ignoresSafeArea()
.onAppear {
vm.checkIfLocationServicesEnabled()
}
VStack(spacing: 0) {
header
.padding()
Spacer()
ZStack {
if vm.selectedEntity != nil {
MapCardView(entity: vm.selectedEntity!)
.shadow(color: Color.black.opacity(0.3), radius: 20)
.padding()
.transition(.asymmetric(
insertion: .move(edge: .trailing),
removal: .move(edge: .leading)))
}
}
backButton
}
}
.navigationBarBackButtonHidden()
.navigationBarTitleDisplayMode(.inline)
}
}
1条答案
按热度按时间5f0d552i1#
您可以将它们合并到位置的计算属性中
我在这里使用CLLocationCoordinate2D作为元素,但MapAnnotation可能同样有效。