我需要将数千个注释覆盖到MapKit地图上。它们在被映射之前的最后一站将位于如下所示的结构数组中。
我似乎不知道如何将
Map
Annotation
嵌入到 Map
将接受的循环中。
import SwiftUI
import MapKit
struct MyAnnotation: Identifiable {
let id = UUID()
let name: String
let coordinate: CLLocationCoordinate2D
let color: Color
let score: Double
}
struct JunkMapView: View {
let annotations = [
MyAnnotation(name: "9", coordinate: CLLocationCoordinate2D(latitude: 42.2926854029081, longitude: -71.0679634411345), color: .yellow, score: 10),
MyAnnotation(name: "8", coordinate: CLLocationCoordinate2D(latitude: 42.4281694052464, longitude: -71.1093263739832), color: .orange, score: 7.2),
MyAnnotation(name: "7", coordinate: CLLocationCoordinate2D(latitude: 42.3717488333676, longitude: -71.0670886507071), color: .red, score: 3.46)
]
var body: some View {
Text("Hello, World!")
Map {
Annotation("", coordinate: annotations[1].coordinate, anchor: .bottom) {
HStack{
Image(systemName: "flame.circle")
.symbolRenderingMode(.palette)
.foregroundStyle(annotations[1].color, .blue)
.font(.system(size: 100, weight: .thin))
.padding(4)
.background(.clear)
Text("\(annotations[1].score)")
}
}
}
}
}
#Preview {
JunkMapView()
}
使用
ForEach
循环尝试此方法来显示所有注释:
Map {
ForEach(annotations) { p in
Annotation("", coordinate: p.coordinate) {
HStack{
Image(systemName: "flame.circle")
.symbolRenderingMode(.palette)
.foregroundStyle(p.color, .blue)
.font(.system(size: 100, weight: .thin))
.padding(4)
.background(.clear)
Text("\(p.score)")
}
}
}
}