BuildingListAndNavigation

I would like to add multiple Annotations to the Map of the 2020 Apple Tutorial "BuildingListAndNavigation".
I'd like to do it in the way implementing an LandmarkData.json-File in the Bundle.
Question from a beginner: how?
I searched the web without finding an example in SwiftUI.
Do you have codeSnippets for the different swift-files, and how to connect them?

Replies

Code Block class LandmarkAnnotation : NSObject, MKAnnotation {
    
    let title: String?
    let subtitle: String?
    let coordinate: CLLocationCoordinate2D
    
                init(title:      String?,
                     subtitle:   String?,
                     coordinate: CLLocationCoordinate2D)
                     {self.title = title
                      self.subtitle = subtitle
                      self.coordinate = coordinate}
    
    static func requestMockData()-> [LandmarkAnnotation]{
           return [
              LandmarkAnnotation(title: "Aachen",
                                  subtitle: "Karls Dom",
                                  coordinate: .init(latitude: 50.774910, longitude: 6.083951)),
               LandmarkAnnotation(title: "Vaals",
                                  subtitle: "Aachens `kleine Schwerster` in den Niederlanden",
                                  coordinate: .init(latitude: 50.773437, longitude: 6.016159)),
               LandmarkAnnotation(title: "Köln",
                                  subtitle: "Dom",
                                  coordinate: .init(latitude: 50.941278, longitude: 6.958281)),
               LandmarkAnnotation(title: "Ponttor",
                                    subtitle: "Teil der ehemlaigen Stadtmauer",
                                    coordinate: .init(latitude: 50.781770, longitude: 6.078248)),
               LandmarkAnnotation(title: "Dreiländereck",
                                  subtitle: "NE,BE, D",
                                  coordinate: .init(latitude: 50.754482, longitude: 6.020776))
            
           ]
       }
}


Code Block import SwiftUI
struct LandmarkDetail: View {
    
    var landmark: Landmark
    
    var body: some View {
        
        VStack{
                MapView(coordinate: landmark.locationCoordinate)
               
                .edgesIgnoringSafeArea(.top)
                .frame(height: 300)
            
            CircleImage(image: landmark.image)
                .offset(x: 0, y: -130)
                .padding(.bottom, -130)
 
            VStack(alignment: .leading) {
                Text(landmark.name)
                    .font(.title)
                HStack(alignment: .top) {
                    Text(landmark.park)
                        .font(.subheadline)
                    Spacer()
                    Text(landmark.state)
                        .font(.subheadline)
                }
            }
            .padding()
            Spacer()
        }
        .navigationBarTitle(Text(landmark.name), displayMode: .inline)
    }
}
struct LandmarkDetail_Previews: PreviewProvider {
    static var previews: some View {
        LandmarkDetail(landmark: landmarkData[0])
    }
}


Code Block import SwiftUI
import MapKit
struct MapView: UIViewRepresentable {
    var coordinate: CLLocationCoordinate2D
    
    let landmarks = LandmarkAnnotation.requestMockData()
  
    func makeUIView(context: Context) -> MKMapView {
        MKMapView(frame: .zero)
    }
    
    
    func updateUIView(_ view: MKMapView, context: Context) {
        let span = MKCoordinateSpan(latitudeDelta: 0.02, longitudeDelta: 0.02)
        let region = MKCoordinateRegion(center: coordinate, span: span)
        view.setRegion(region, animated: true)
        /
        view.addAnnotations(landmarks)
    }
}
struct MapView_Previews: PreviewProvider {
    static var previews: some View {
        MapView(coordinate: landmarkData[0].locationCoordinate)
        
    }
}