英文:
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? // <--- 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 // <--- here
} label: {
Text("E")
.frame(width: 40, height: 40)
.foregroundColor(.white)
.background(.blue)
}
}
}
}
.sheet(item: $selected){ annotation in // <--- here
EventView(eventLoc: annotation.coordinates)
}
.navigationTitle("Map")
.edgesIgnoringSafeArea(.all)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论