为什么我的地图标注都传递相同的结果给我的视图?

huangapple go评论69阅读模式
英文:

Why are my map annotations all passing the same results to my view?

问题

每个注释都将相同的位置传递给EventView,而不是它们各自的坐标。每个坐标应该有一个单独的页面,包含独特的信息。如果代码放错了地方,我很抱歉。在此提前致谢。

英文:

Each annotation is passing the same location to EventView instead of their individual coordinates. Each coordinate should have a separate page with unique information. Sorry if the code is put in wrong. Thank you in advance.

ZStack(alignment: .bottom) {
    Map(coordinateRegion: .constant(locationManager.region) ,showsUserLocation: true, annotationItems: annotations){annotation in MapAnnotation(coordinate: annotation.coordinates){
           if annotation.event == true{
                 Button(){
                     readyToEvent = true
                 }label: {
                     Text("E")
                     .frame(width: 40, height: 40)
                     .foregroundColor(.white)
                     .background(.blue)
                 }
                     .sheet(isPresented: $readyToEvent){EventView(eventLoc: annotation.coordinates)}
                            }
                        }
                    }
                    .edgesIgnoringSafeArea(.all)
                    .accentColor(Color.red)
                    .onAppear(perform: locationManager.requestLoc)
                    .onAppear(perform: fetchLocations)
}

答案1

得分: 1

尝试这种方法,使用更合适的 .sheet(item: ...)。将它放在Map(coordinateRegion: ...)之外,并使用@State var selected: YourAnnotationType?作为选定的注释,传递给从Button获取的EventView

@State var selected: YourAnnotationType?  // <--- 这里
// ...

var body: some View {
    //....
    Map(coordinateRegion: .constant(locationManager.region), showsUserLocation: true, annotationItems: annotations) { annotation in

        MapAnnotation(coordinate: annotation.coordinates) {
            if annotation.event {
                Button {
                    selected = annotation  // <--- 这里
                } label: {
                    Text("E")
                        .frame(width: 40, height: 40)
                        .foregroundColor(.white)
                        .background(Color.blue)
                }
            }
        }
    }
    .sheet(item: $selected) { annotation in  // <--- 这里
        EventView(eventLoc: annotation.coordinates)
    }
    .navigationTitle("地图")
    .edgesIgnoringSafeArea(.all)
}

希望这对你有所帮助。

英文:

Try this approach, where you use the more appropriate .sheet(item: ...). Place it outside
the Map(coordinateRegion: ...), and use a @State var selected: YourAnnotationType?
as the selected annotation to pass to the EventView obtained from the Button.

@State var selected: YourAnnotationType?  // &lt;--- here
// ...
 
var body: some View {
    //....
    Map(coordinateRegion: .constant(locationManager.region) ,showsUserLocation: true, annotationItems: annotations) { annotation in

        MapAnnotation(coordinate: annotation.coordinates) {
            if annotation.event {
                Button{
                    selected = annotation  // &lt;--- here
                } label: {
                    Text(&quot;E&quot;)
                        .frame(width: 40, height: 40)
                        .foregroundColor(.white)
                        .background(.blue)
                }
            }
        }
    }
    .sheet(item: $selected){ annotation in  // &lt;--- here
        EventView(eventLoc: annotation.coordinates)
    }
    .navigationTitle(&quot;Map&quot;)
    .edgesIgnoringSafeArea(.all)
}

huangapple
  • 本文由 发表于 2023年3月7日 16:20:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75659487.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定