在 SwiftUI 中点击 MKMapView
Tapping an MKMapView in SwiftUI
我在 SwiftUI 应用程序中有一张地图。它正在发挥作用;但现在我希望能够点击它并知道点击的纬度和经度。这是当前代码:
import SwiftUI
import MapKit
struct MapView: UIViewRepresentable {
@Binding var centerCoordinate: CLLocationCoordinate2D
func makeUIView(context: Context) -> MKMapView {
let mapView = MKMapView()
mapView.delegate = context.coordinator
let gRecognizer = UITapGestureRecognizer(target: context.coordinator,
action: #selector(Coordinator.tapHandler(_:)))
mapView.addGestureRecognizer(gRecognizer)
return mapView
}
func updateUIView(_ view: MKMapView, context: Context) {
//print(#function)
}
func makeCoordinator() -> Coordinator {
return Coordinator(self)
}
class Coordinator: NSObject, MKMapViewDelegate {
var parent: MapView
init(_ parent: MapView) {
self.parent = parent
}
let gRecognizer = UITapGestureRecognizer(target: self,
action: #selector(tapHandler(_:)))
@objc func tapHandler(_ gesture: UITapGestureRecognizer) {
print(#function)
.... get useful information here ...
}
}
}
在这种状态下,我可以看到我点击的时间,但我没有得到我需要的信息(即点击的坐标)。
在网上搜索后,我尝试了一些代码变体。
此时它还没有工作。任何有关前进道路的相关提示都将非常受欢迎。
我也遇到过类似的情况,我就是这样做的。
我制作了 Coordinator UIGestureRecognizerDelegate,并确保为其设置了 gRecognizer 委托,并将其添加到地图中。类似于:
struct MapView: UIViewRepresentable {
@Binding var centerCoordinate: CLLocationCoordinate2D
let mapView = MKMapView()
func makeUIView(context: Context) -> MKMapView {
mapView.delegate = context.coordinator
return mapView
}
func updateUIView(_ view: MKMapView, context: Context) {
//print(#function)
}
func makeCoordinator() -> Coordinator {
return Coordinator(self)
}
class Coordinator: NSObject, MKMapViewDelegate, UIGestureRecognizerDelegate {
var parent: MapView
var gRecognizer = UITapGestureRecognizer()
init(_ parent: MapView) {
self.parent = parent
super.init()
self.gRecognizer = UITapGestureRecognizer(target: self, action: #selector(tapHandler))
self.gRecognizer.delegate = self
self.parent.mapView.addGestureRecognizer(gRecognizer)
}
@objc func tapHandler(_ gesture: UITapGestureRecognizer) {
// position on the screen, CGPoint
let location = gRecognizer.location(in: self.parent.mapView)
// position on the map, CLLocationCoordinate2D
let coordinate = self.parent.mapView.convert(location, toCoordinateFrom: self.parent.mapView)
}
}
}
我在 SwiftUI 应用程序中有一张地图。它正在发挥作用;但现在我希望能够点击它并知道点击的纬度和经度。这是当前代码:
import SwiftUI
import MapKit
struct MapView: UIViewRepresentable {
@Binding var centerCoordinate: CLLocationCoordinate2D
func makeUIView(context: Context) -> MKMapView {
let mapView = MKMapView()
mapView.delegate = context.coordinator
let gRecognizer = UITapGestureRecognizer(target: context.coordinator,
action: #selector(Coordinator.tapHandler(_:)))
mapView.addGestureRecognizer(gRecognizer)
return mapView
}
func updateUIView(_ view: MKMapView, context: Context) {
//print(#function)
}
func makeCoordinator() -> Coordinator {
return Coordinator(self)
}
class Coordinator: NSObject, MKMapViewDelegate {
var parent: MapView
init(_ parent: MapView) {
self.parent = parent
}
let gRecognizer = UITapGestureRecognizer(target: self,
action: #selector(tapHandler(_:)))
@objc func tapHandler(_ gesture: UITapGestureRecognizer) {
print(#function)
.... get useful information here ...
}
}
}
在这种状态下,我可以看到我点击的时间,但我没有得到我需要的信息(即点击的坐标)。 在网上搜索后,我尝试了一些代码变体。 此时它还没有工作。任何有关前进道路的相关提示都将非常受欢迎。
我也遇到过类似的情况,我就是这样做的。 我制作了 Coordinator UIGestureRecognizerDelegate,并确保为其设置了 gRecognizer 委托,并将其添加到地图中。类似于:
struct MapView: UIViewRepresentable {
@Binding var centerCoordinate: CLLocationCoordinate2D
let mapView = MKMapView()
func makeUIView(context: Context) -> MKMapView {
mapView.delegate = context.coordinator
return mapView
}
func updateUIView(_ view: MKMapView, context: Context) {
//print(#function)
}
func makeCoordinator() -> Coordinator {
return Coordinator(self)
}
class Coordinator: NSObject, MKMapViewDelegate, UIGestureRecognizerDelegate {
var parent: MapView
var gRecognizer = UITapGestureRecognizer()
init(_ parent: MapView) {
self.parent = parent
super.init()
self.gRecognizer = UITapGestureRecognizer(target: self, action: #selector(tapHandler))
self.gRecognizer.delegate = self
self.parent.mapView.addGestureRecognizer(gRecognizer)
}
@objc func tapHandler(_ gesture: UITapGestureRecognizer) {
// position on the screen, CGPoint
let location = gRecognizer.location(in: self.parent.mapView)
// position on the map, CLLocationCoordinate2D
let coordinate = self.parent.mapView.convert(location, toCoordinateFrom: self.parent.mapView)
}
}
}