在这里,我如何能够找到长老会的位置,同时保持马普吉斯先发制人<代码>选取/代码”参数的功能(即在现有地图标上不许长时间):
struct MapLongPressTestView: View {
@State private var selectedMapItemTag: String? = nil
private let randomCoordinate = CLLocationCoordinate2D(latitude: .random(in: -90...90), longitude: .random(in: -180...180))
var body: some View {
MapReader { proxy in
Map(selection: $selectedMapItemTag) {
// Your Annotations, Markers, Shapes, & other map content
// Selectable Test Example
Annotation(coordinate: randomCoordinate) {
Image(systemName: "water.waves")
.foregroundStyle(.orange)
} label: {
Text("Water, most likely")
}
.tag("example")
}
.gesture(DragGesture())
.gesture(
LongPressGesture(minimumDuration: 1, maximumDistance: 0)
.sequenced(before: SpatialTapGesture(coordinateSpace: .local))
.onEnded { value in
switch value {
case let .second(_, tapValue):
guard let point = tapValue?.location else {
print("Unable to retreive tap location from gesture data.")
return
}
guard let coordinate = proxy.convert(point, from: .local) else {
print("Unable to convert local point to coordinate on map.")
return
}
print("Long press occured at: (coordinate)")
default: return
}
}
)
}
.onChange(of: selectedMapItemTag) {
print(selectedMapItemTag.map { "($0) is selected" } ?? "No selection")
}
}
}
或者,此处是<代码>Map的延伸,以减少碎块:
extension Map {
func onLongPressGesture(minimumDuration: Double = 0, maximumDistance: CGFloat = 0, onTouchUp: @escaping (CGPoint) -> Void) -> some View {
self
.gesture(DragGesture())
.gesture(
LongPressGesture(minimumDuration: minimumDuration, maximumDistance: maximumDistance)
.sequenced(before: SpatialTapGesture(coordinateSpace: .local))
.onEnded { value in
switch value {
case .second(_, let tapValue):
guard let point = tapValue?.location else {
print("Unable to retreive tap location from gesture data.")
return
}
onTouchUp(point)
default: return
}
}
)
}
}
这样做是为了:
MapReader { proxy in
Map(selection: $selectedMapItemTag) {
// ...
}
.onLongPressGesture(minimumDuration: 1) { point in
if let coordinate = proxy.convert(point, from: .local) {
print("Long press occured at: (coordinate)")
}
}
}
SpacialTapGesture
,可在Si 16+查阅。
Hope this helps!