您如何让用户向地图添加图钉并在 SwiftUI 中获取坐标?

kbe*_*r46 7 mapkit mkmapview mkannotation mkmapviewdelegate swiftui

我在以芝加哥为中心的视图中有一张地图。我希望用户能够在地图上放置图钉/注释,然后检索这些坐标。地图加载芝加哥很好,但我无法使注释代码起作用。

我似乎找不到 SwiftUI 的答案,特别是。只有 Swift 和 Storyboards。我觉得我有 99% 的代码,但这些片段不在正确的位置。我包含了错误所在位置的屏幕截图。谢谢你的帮助。

import SwiftUI
import MapKit

struct EntireMapView: UIViewRepresentable {

    func updateUIView(_ mapView: MKMapView, context: Context) {

        mapView.delegate = context.coordinator

        let longPressGesture = UILongPressGestureRecognizer(target: mapView, action: #selector(EntireMapViewCoordinator.addAnnotation(gesture:)))
        mapView.addGestureRecognizer(longPressGesture)

        let span = MKCoordinateSpan(latitudeDelta: 0.3, longitudeDelta: 0.3)

        var chicagoCoordinate = CLLocationCoordinate2D()
        chicagoCoordinate.latitude = 41.878113
        chicagoCoordinate.longitude = -87.629799

        let region = MKCoordinateRegion(center: chicagoCoordinate, span: span)

        mapView.setRegion(region, animated: true)

    }

    func makeUIView(context: Context) -> MKMapView {

        let mapView = MKMapView(frame: .zero)
        mapView.delegate = context.coordinator
        return mapView

    }

    func makeCoordinator() -> EntireMapViewCoordinator {
        return EntireMapViewCoordinator(self)
    }

    class EntireMapViewCoordinator: NSObject, MKMapViewDelegate {

        var entireMapViewController: EntireMapView

        init(_ control: EntireMapView) {
          self.entireMapViewController = control
        }

        func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
            return (annotation as? MKAnnotationView)!
        }

        @objc func addAnnotation(gesture: UILongPressGestureRecognizer) {

            if gesture.state == .ended {

                let point = gesture.location(in: self.mapView) **<--- ERROR HERE**
                let coordinate = mapView.convert(point, toCoordinateFrom: mapView)

                var annotation = MKPointAnnotation()
                annotation.coordinate = coordinate

                self.mapView.addAnnotation(annotation) **<--- ERROR HERE**
            }
        }
    }
}

struct EntireMapView_Previews: PreviewProvider {
    static var previews: some View {
        EntireMapView()
    }
}
Run Code Online (Sandbox Code Playgroud)

这是错误的屏幕截图

0nd*_*re_ 5

这是答案的完整代码。功劳主要归功于@yeezy,因为使用这行获取视图:let mapView = gestureRecognizer.view as? MKMApView是问题中的主要错误。

  struct EntireMapView: UIViewRepresentable {

        func updateUIView(_ mapView: MKMapView, context: Context) {

            let span = MKCoordinateSpan(latitudeDelta: 0.3, longitudeDelta: 0.3)
            var chicagoCoordinate = CLLocationCoordinate2D()
            chicagoCoordinate.latitude = 41.878113
            chicagoCoordinate.longitude = -87.629799
            let region = MKCoordinateRegion(center: chicagoCoordinate, span: span)
            mapView.setRegion(region, animated: true)

        }

        func makeUIView(context: Context) -> MKMapView {

            let myMap = MKMapView(frame: .zero)
            let longPress = UILongPressGestureRecognizer(target: context.coordinator, action: #selector(EntireMapViewCoordinator.addAnnotation(gesture:)))
            longPress.minimumPressDuration = 1
            myMap.addGestureRecognizer(longPress)
            myMap.delegate = context.coordinator
            return myMap

        }

    func makeCoordinator() -> EntireMapViewCoordinator {
        return EntireMapViewCoordinator(self)
    }

    class EntireMapViewCoordinator: NSObject, MKMapViewDelegate {

        var entireMapViewController: EntireMapView

        init(_ control: EntireMapView) {
          self.entireMapViewController = control
        }


        @objc func addAnnotation(gesture: UIGestureRecognizer) {

            if gesture.state == .ended {

                if let mapView = gesture.view as? MKMapView {
                let point = gesture.location(in: mapView)
                let coordinate = mapView.convert(point, toCoordinateFrom: mapView)
                let annotation = MKPointAnnotation()
                annotation.coordinate = coordinate
                mapView.addAnnotation(annotation)
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


Ada*_*lin 0

我将从删除mapView.delegate = context.coordinatorupdateUIView 开始。这应该只在 makeUIView 中完成一次(您已经在这样做了)。