如何在触摸时向 MKMapView(IOS) 添加图钉?
我必须获取用户在 MKMapView 上触摸的点的坐标。 我不使用 Interface Builder。 你能给我举一个例子吗?
I had to get the coordinate of a point where the user touch on a MKMapView.
I'm not working with the Interface Builder.
Can you give me one example?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 UILongPressGestureRecognizer 来实现此目的。无论您在何处创建或初始化地图视图,首先将识别器附加到它:
然后在手势处理程序中:
YourMKAnnotationClass 是您定义的类,它符合 MKAnnotation 协议。如果您的应用程序仅在 iOS 4.0 或更高版本上运行,您可以使用预定义的 MKPointAnnotation< /a> 类。
有关创建您自己的 MKAnnotation 类的示例,请参阅示例应用 MapCallouts。
You can use a UILongPressGestureRecognizer for this. Wherever you create or initialize the mapview, first attach the recognizer to it:
Then in the gesture handler:
YourMKAnnotationClass is a class you define that conforms to the MKAnnotation protocol. If your app will only be running on iOS 4.0 or later, you can use the pre-defined MKPointAnnotation class instead.
For examples on creating your own MKAnnotation class, see the sample app MapCallouts.
感谢安娜提供了如此好的答案!如果有人感兴趣的话,这里有一个 Swift 版本(答案已更新为 Swift 4.1 语法)。
创建 UILongPressGestureRecognizer:
处理手势:
Thanks to Anna for providing such a great answer! Here is a Swift version if anybody is interested (the answer has been updated to Swift 4.1 syntax).
Creating UILongPressGestureRecognizer:
Handling the gesture: