英文:
Get coordinates in the onLongPressGesture on the Map
问题
我的问题与这个问题非常相似:https://stackoverflow.com/questions/62837754/capture-touchdown-location-of-onlongpressgesture-in-swiftui
然而,有一个显著的特殊之处:我想获取 Map
上 .onLongPressGesture
的坐标。原问题中的解决方法在这种情况下有两个主要问题:
-
它破坏了默认的
Map
拖动功能。 -
它在“释放”时捕获坐标,而不是在“按住 X 秒” 时。我想在按住配置的秒数(
minimumDuration
参数)后,但在抬起手指之前获得坐标。
我尝试了不同手势的不同组合顺序:.onTapGesture
,DragGesture
等,但无法以可行的方式将它们组合在一起(要么破坏地图的拖动,要么在抬起手指时检测到坐标,要么出现其他问题)。
此外,我尝试创建一个透明覆盖层,并在那里应用一些手势,但也无法成功...
我还看过这个问题(和答案),但没有帮助,因为它没有捕获坐标(而且,如果我没记错的话,它也破坏了拖动功能):https://stackoverflow.com/questions/71399626/anybody-got-swiftuimapkitlongpress-working
英文:
My question is very similar to this one: https://stackoverflow.com/questions/62837754/capture-touchdown-location-of-onlongpressgesture-in-swiftui
However, there's one significant peculiarity: I want to get the coordinates of the .onLongPressGesture
on the Map
. The solution in the original question has two major issues in this case:
-
It breaks the default
Map
dragging functionality. -
It captures the coordinate on "release" - not on "press and hold for X seconds". I want to get coordinates after pressing and holding for the configured amount of seconds (
minimumDuration
parameter), but before lifting the finger.
I tried different combinations of gestures in different orders: .onTapGesture
, DragGesture
, etc, but couldn't combine them in a workable way (either dragging of the map gets broken, or the coordinate is detected upon the finger lifting, or some other issues).
Also, I tried to create a transparent overlay and apply some gestures there, but didn't manage either...
I've also seen this question (and answer), but it doesn't help, because it doesn't capture the coordinates (and, IIRC, it also broke the dragging functionality): https://stackoverflow.com/questions/71399626/anybody-got-swiftuimapkitlongpress-working
答案1
得分: 0
基于这里没有答案(Apple开发者论坛上也没有答案),而且我的进一步实验都没有成功,我认为,很不幸,使用Map
组件无法实现所需的行为。我不得不退回到MKMapView
和UILongPressGestureRecognizer
的组合。有关更多详细信息,请参阅此问题的答案:https://stackoverflow.com/questions/30858360/adding-a-pin-annotation-to-a-map-view-on-a-long-press-in-swift
英文:
Based on the fact that there are no answers here (and no answers on Apple Developer Forum either) and none of my further experiments succeeded, I assume that it's, unfortunately, not possible to achieve the desired behaviour with the Map
component. I had to fall-back to the combination of MKMapView
and UILongPressGestureRecognizer
. More details can be found in the answers to this question: https://stackoverflow.com/questions/30858360/adding-a-pin-annotation-to-a-map-view-on-a-long-press-in-swift
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论