我无法打印我在地图标记上拉出的 api
I can't print the api I pulled on the mapmarker
我从地图标记中提取数据,当我打印时,我看到了所有这些数据,但我无法从示例中获取它们 (quake.latitude)。错误。即使它是一个列表,我也通过在括号中输入“(quakes) { quake in”来获取它,但现在我不知道如何通过 mapview 来实现。
struct MapAnnotationsView: View {
@State var quakes: [EarthQuake] = []
@State private var region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 38.9520281, longitude: 35.6980142), span: MKCoordinateSpan(latitudeDelta: 30, longitudeDelta: 10))
let placeArray: [Place] = [Place(title: {quake.latitude}, coordinate: CLLocationCoordinate2D(latitude: 37.8008, longitude: 27.2465))]
var body: some View {
Map(coordinateRegion: $region, annotationItems: placeArray) { annotation in
// This makes a generic annotation that takes a View
MapAnnotation(coordinate: annotation.coordinate) {
// This is your custom view
AnnotationView(placeName: annotation.title)
}
} .onAppear {
Api().getEarthQuake { (quakes) in
self.quakes = quakes
}
}
}
}
Error code screenshot
试试这个:
struct MapAnnotationsView: View {
@State private var region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 38.9520281, longitude: 35.6980142), span: MKCoordinateSpan(latitudeDelta: 30, longitudeDelta: 10))
@State private var placeArray: [Place] = []
var body: some View {
Map(coordinateRegion: $region, annotationItems: placeArray) { annotation in
// This makes a generic annotation that takes a View
MapAnnotation(coordinate: annotation.coordinate) {
// This is your custom view
AnnotationView(placeName: annotation.title)
}
} .onAppear {
Api().getEarthQuake { (quakes) in
let tempArray = quakes.map{ quake in
Place(title: "\(quake.latitude)", coordinate: CLLocationCoordinate2D(latitude: 37.8008, longitude: 27.2465))
}
self.placeArray = tempArray
}
}
}
}
由于这不是一个可重现的示例,因此此处可能存在一些拼写错误,您必须自己解决。
解释:
您的 属性 placeArray
不能依赖于另一个 属性。要获得一系列地点以提供给您的地图,您需要在从 API 加载地震并分配它们后创建这些地点。使用 @State var
确保您的视图得到更新。
我从地图标记中提取数据,当我打印时,我看到了所有这些数据,但我无法从示例中获取它们 (quake.latitude)。错误。即使它是一个列表,我也通过在括号中输入“(quakes) { quake in”来获取它,但现在我不知道如何通过 mapview 来实现。
struct MapAnnotationsView: View {
@State var quakes: [EarthQuake] = []
@State private var region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 38.9520281, longitude: 35.6980142), span: MKCoordinateSpan(latitudeDelta: 30, longitudeDelta: 10))
let placeArray: [Place] = [Place(title: {quake.latitude}, coordinate: CLLocationCoordinate2D(latitude: 37.8008, longitude: 27.2465))]
var body: some View {
Map(coordinateRegion: $region, annotationItems: placeArray) { annotation in
// This makes a generic annotation that takes a View
MapAnnotation(coordinate: annotation.coordinate) {
// This is your custom view
AnnotationView(placeName: annotation.title)
}
} .onAppear {
Api().getEarthQuake { (quakes) in
self.quakes = quakes
}
}
}
}
Error code screenshot
试试这个:
struct MapAnnotationsView: View {
@State private var region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 38.9520281, longitude: 35.6980142), span: MKCoordinateSpan(latitudeDelta: 30, longitudeDelta: 10))
@State private var placeArray: [Place] = []
var body: some View {
Map(coordinateRegion: $region, annotationItems: placeArray) { annotation in
// This makes a generic annotation that takes a View
MapAnnotation(coordinate: annotation.coordinate) {
// This is your custom view
AnnotationView(placeName: annotation.title)
}
} .onAppear {
Api().getEarthQuake { (quakes) in
let tempArray = quakes.map{ quake in
Place(title: "\(quake.latitude)", coordinate: CLLocationCoordinate2D(latitude: 37.8008, longitude: 27.2465))
}
self.placeArray = tempArray
}
}
}
}
由于这不是一个可重现的示例,因此此处可能存在一些拼写错误,您必须自己解决。
解释:
您的 属性 placeArray
不能依赖于另一个 属性。要获得一系列地点以提供给您的地图,您需要在从 API 加载地震并分配它们后创建这些地点。使用 @State var
确保您的视图得到更新。