Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am making a Map view and would like to show a detail view sheet when you tap on each annotation item on the map. The annotations are locations of National Parks. I have a local JSON file that I am using to populate these views. The problem I am having is that every time I tap one of the annotations, it shows the wrong detail view. In fact, it shows the same detail view for every annotation. I need it to bring up the corresponding detail view for that specific National Park. I have attached my code below as well as two images to help you better understand my problem. I am very new to coding, only been at it just over a month so if you have any tips for me I would really appreciate it!

struct MapView: View {
    //PROPERTIES
    @State private var region: MKCoordinateRegion = {
        var mapCoordinates = CLLocationCoordinate2D(latitude: 39.8283, longitude: -98.5795)
        var mapZoomLevel = MKCoordinateSpan(latitudeDelta: 60.0, longitudeDelta: 60.0)
        var mapRegion = MKCoordinateRegion(center: mapCoordinates, span: mapZoomLevel)
        return mapRegion
    }()
    @State private var isShowingDetailView : Bool = false
    
    var parks : [Park] = Bundle.main.decode("parkInfo.json")
    
    
    //BODY
    var body: some View {
        HStack {
            Map(coordinateRegion: $region, annotationItems: parks, annotationContent: {
                park in
                MapAnnotation(coordinate: park.location) {
                    Button(action: {
                        self.isShowingDetailView.toggle()
                    }) {
                        MapAnnotationView(park: park)
                    }
                    .sheet(isPresented: $isShowingDetailView) {
                        DetailView(park: park)
                            
                    }
                }
            })
            
            .edgesIgnoringSafeArea(.top)
        } //: HSTACK

    }
} 

Map View: Tap on annotations to bring up detail view:

image

Sheet presented after tapping annotation:

image

question from:https://stackoverflow.com/questions/65928037/map-annotation-buttons-to-show-a-sheet-swiftui

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
440 views
Welcome To Ask or Share your Answers For Others

1 Answer

Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...