添加地图标记 api

Adding map marker api

我通过 mapkit 添加了地图标记,但我将通过 api 提取经纬度。当我写信添加 api 时,出现多个错误。

struct MapAnnotationsView: View {
@State private var region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 38.9520281, longitude: 35.6980142), span: MKCoordinateSpan(latitudeDelta: 30, longitudeDelta: 10))

let placeArray: [Place] = [Place(title: "Deprem Test", coordinate: CLLocationCoordinate2D(latitude: 37.8008, longitude: 27.2465))]

var body: some View {
    Map(coordinateRegion: $region, annotationItems: placeArray)
        .onAppear {
                        Api().getEarthQuake { (quakes) in
                            self.quakes = quakes
                        }
                    }
    { annotation in
        // This makes a generic annotation that takes a View
        MapAnnotation(coordinate: annotation.coordinate) {
            // This is your custom view
            AnnotationView(placeName: annotation.title)
        }
    }
}
}

Code error screenshot

onAppear 及其闭包必须在 { annotation in ... } 闭包之后进行,因为后者必须附加到 Map.

struct MapAnnotationsView: View {
    @State private var region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 38.9520281, longitude: 35.6980142), span: MKCoordinateSpan(latitudeDelta: 30, longitudeDelta: 10))
    @State var quakes: [EarthQuake] = []
    
    
    let placeArray: [Place] = [Place(title: "Deprem Test", 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
            }
        }
    }
}