如何让我的地图注释打开正确的视图? - Swift
How would I make my map annotation open the correct view? - Swift
我正在尝试做到这一点,以便每次单击我的地图注释时都会出现相应的视图,但似乎并非如此。它看起来像是混合在一起,有时会多次出现相同的视图。这是我到目前为止的代码:
Map(coordinateRegion: $region, interactionModes: .all, showsUserLocation: true, userTrackingMode: nil, annotationItems: sclocations) { item in
MapAnnotation(coordinate: item.location) {
Button(action: {
self.activeSheet = .sheetA
}, label: {
Image(systemName: "mappin")
.foregroundColor(.red)
}) //: BUTTON
.sheet(item: $activeSheet) { sheet in
switch sheet {
case .sheetA:
SCDetailView(sclocations: item)
}
}
}
}
我将不得不在这里做出一些假设,因为您没有显示 item
的类型。假设它叫做 LocationItem
。重要的是它符合Identifiable
:
struct LocationItem : Identifiable {
var id = UUID()
var name : String
var location: CLLocationCoordinate2D
}
您需要一个 @State
变量来在您的视图中存储一个可选的 LocationItem
:
@State var locationItem : LocationItem?
您的按钮将设置 locationItem
:
Button(action: {
self.locationItem = item
})
您的 sheet
调用将如下所示:
.sheet(item: $locationItem) { locationItem in
SCDetailView(sclocations: locationItem) //note how I'm using locationItem here -- the parameter from the closure
}
最后,我将 sheet
移动到 外部 你的 MapAnnotation
这样你的整个视图主体看起来更像:
Map(coordinateRegion: $region, interactionModes: .all, showsUserLocation: true, userTrackingMode: nil, annotationItems: sclocations) { item in
MapAnnotation(coordinate: item.location) {
Button(action: {
self.locationItem = item
}) {
Image(systemName: "mappin").foregroundColor(.red)
}
}
}
.sheet(item: $locationItem) { locationItem in
SCDetailView(sclocations: locationItem)
}
请记住,由于您没有像我说的那样提供完整的代码示例,所以我在猜测一些事情,因此您可能必须将其外推到您自己的解决方案中(比如将您的类型设为 Identifiable
).
我正在尝试做到这一点,以便每次单击我的地图注释时都会出现相应的视图,但似乎并非如此。它看起来像是混合在一起,有时会多次出现相同的视图。这是我到目前为止的代码:
Map(coordinateRegion: $region, interactionModes: .all, showsUserLocation: true, userTrackingMode: nil, annotationItems: sclocations) { item in
MapAnnotation(coordinate: item.location) {
Button(action: {
self.activeSheet = .sheetA
}, label: {
Image(systemName: "mappin")
.foregroundColor(.red)
}) //: BUTTON
.sheet(item: $activeSheet) { sheet in
switch sheet {
case .sheetA:
SCDetailView(sclocations: item)
}
}
}
}
我将不得不在这里做出一些假设,因为您没有显示 item
的类型。假设它叫做 LocationItem
。重要的是它符合Identifiable
:
struct LocationItem : Identifiable {
var id = UUID()
var name : String
var location: CLLocationCoordinate2D
}
您需要一个 @State
变量来在您的视图中存储一个可选的 LocationItem
:
@State var locationItem : LocationItem?
您的按钮将设置 locationItem
:
Button(action: {
self.locationItem = item
})
您的 sheet
调用将如下所示:
.sheet(item: $locationItem) { locationItem in
SCDetailView(sclocations: locationItem) //note how I'm using locationItem here -- the parameter from the closure
}
最后,我将 sheet
移动到 外部 你的 MapAnnotation
这样你的整个视图主体看起来更像:
Map(coordinateRegion: $region, interactionModes: .all, showsUserLocation: true, userTrackingMode: nil, annotationItems: sclocations) { item in
MapAnnotation(coordinate: item.location) {
Button(action: {
self.locationItem = item
}) {
Image(systemName: "mappin").foregroundColor(.red)
}
}
}
.sheet(item: $locationItem) { locationItem in
SCDetailView(sclocations: locationItem)
}
请记住,由于您没有像我说的那样提供完整的代码示例,所以我在猜测一些事情,因此您可能必须将其外推到您自己的解决方案中(比如将您的类型设为 Identifiable
).