swiftui - SwiftUI Map 长按注解
问题描述
用户如何通过长按向地图添加注释?我无法从地图视图中获取位置(纬度、经度)?你能通过 SwiftUI 解释一下吗?
let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(addAnnotationOnLongPress(gesture:)))
longPressGesture.minimumPressDuration = 1.0 self.mapView.addGestureRecognizer(longPressGesture)
@objc func addAnnotationOnLongPress(gesture: UILongPressGestureRecognizer) {
if gesture.state == .ended {
let point = gesture.location(in: self.mapView)
let coordinate = self.mapView.convert(point, toCoordinateFrom: self.mapView)
print(coordinate)
//Now use this coordinate to add annotation on map.
var annotation = MKPointAnnotation()
annotation.coordinate = coordinate
//Set title and subtitle if you want
annotation.title = "Title"
annotation.subtitle = "subtitle"
self.mapView.addAnnotation(annotation)
}
}
我该如何使用它,例如通过 SWIFTUI 的 UILongPressGestureRecognizer?谢谢
解决方案
推荐阅读
- android - 为什么我在 Android 中切换片段后工具栏被忽略?
- multithreading - TCL中线程的正确使用方法
- batch-file - 为什么 FOR LOOP 仅输出 Echo 在 txt 文件中打开?
- django - 如何在不刷新页面的情况下调用函数 django views.py?
- swift - 没有在我的设备上收到通知,只是在控制台中
- sql - 内连接和派生表
- javascript - 在插件调试中有反应调试器
- javascript - 如何将一个div的值写入另一个div
- swift - addSnapshotListener 快速 Firestore 行为
- javascript - 如何在手表模式下进行任何摩卡测试之前运行全局设置脚本