ios 将多个Core Data属性组合并列出到一个数组中-专门用于SwiftUIMap工具包

k7fdbhmy  于 2023-05-19  发布在  iOS
关注(0)|答案(1)|浏览(115)

我在使用@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)
    }
}
5f0d552i

5f0d552i1#

您可以将它们合并到位置的计算属性中

var locations: [CLLocationCoordinate2D] {
    orgs.map { 
        CLLocationCoordinate2D(latitude: $0.lat, longitude: $0.long)
    } +
    people.map { 
        CLLocationCoordinate2D(latitude: $0.lat, longitude: $0.long)
    } +
    tasks.map { 
        CLLocationCoordinate2D(latitude: $0.lat, longitude: $0.long)
    }
}

我在这里使用CLLocationCoordinate2D作为元素,但MapAnnotation可能同样有效。

相关问题