首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何让用户将图钉添加到地图并在SwiftUI中获取坐标?

如何让用户将图钉添加到地图并在SwiftUI中获取坐标?
EN

Stack Overflow用户
提问于 2019-12-05 02:26:08
回答 3查看 1.2K关注 0票数 8

我有一张以芝加哥为中心的地图。我希望用户能够在地图上放置大头针/注释,然后检索这些坐标。地图加载芝加哥很好,但是我不能让注释代码工作。

具体来说,我似乎找不到SwiftUI的答案。只有Swift和Storyboard。我觉得我有99%的代码,但代码片段并不在正确的位置。我包含了错误所在的屏幕截图。谢谢你的帮助。

代码语言:javascript
运行
复制
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()
    }
}

Here's a screen shot of the errors

EN

Stack Overflow用户

发布于 2020-03-23 11:21:26

这是一个完整的答案代码。这主要归功于@yeezy,因为通过这一行获得观点:let mapView = gestureRecognizer.view as? MKMApView是问题中的主要错误。

代码语言:javascript
运行
复制
  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)
                }
            }
        }
    }
}
票数 4
EN
查看全部 3 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59182557

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档