【问题标题】:How to get MKMapView Directions in SwiftUI如何在 SwiftUI 中获取 MKMapView 方向
【发布时间】:2019-12-04 18:40:34
【问题描述】:

在我的应用程序中,我使用 MKMapView SwiftUI 实现。我的地图运行良好,但我想在从 ContentView 中点击按钮时获取路线。我已经在下面更详细地解释了......

这是我的内容视图:

struct ContentView: View {

    var body: some View {
        VStack {
            AppleMapView(coordinate: CLLocationCoordinate2D(latitude: 40.7127, longitude: -74.0059))
            .frame(height: 400)

            Button(action: {
                **// !!! I want to call AppleMapView/getDirections() method here !!!**
            }) {
                Text("Get Directions")
            }
        }
    }
}

这是我的地图视图:

struct AppleMapView: UIViewRepresentable {
    var coordinate: CLLocationCoordinate2D

    func makeUIView(context: Context) -> MKMapView {
        // some codes //
    }

    func updateUIView(_ uiView: MKMapView, context: Context) {
        // some codes //
    }

    func makeCoordinator() -> Coordinator {
        return Coordinator()
    }

    func getDirections() {
        // some codes //
    }

    class Coordinator: NSObject, MKMapViewDelegate {

        func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
            let renderer = MKPolylineRenderer(overlay: overlay)
            renderer.strokeColor = .blue
            renderer.lineWidth = 4
            return renderer
        }

    }
}

谢谢。

【问题讨论】:

  • 您收到什么错误?为什么不能调用那个函数?我们需要更多信息来提供帮助。
  • 我已经更新了问题。我觉得比较好理解。谢谢。
  • 我的回答有帮助吗?

标签: ios swift swiftui ios13 xcode11


【解决方案1】:

MapView 上创建@Binding 属性,然后在Button 操作中从ContentView 设置directions

这样,您的updateUIView 将使用更新后的值相应地调用。

struct ContentView: View {
    @State var directions: [CLLocation] = []

    var body: some View {
        VStack {
            MapView(directions: $directions)

            Button(action: {
                // Directions are Nepal to India
                self.directions = [CLLocation(latitude: 27.2041206, longitude: 84.6093928), CLLocation(latitude: 20.7712763, longitude: 73.7317739)]
            }) {
                Text("Get Directions")
            }
        }
    }
}

struct MapView: UIViewRepresentable {
    @Binding var directions: [CLLocation]

    class Coordinator: NSObject, MKMapViewDelegate {
        var parent: MapView

        init(_ parent: MapView) {
            self.parent = parent
        }

        func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
            let renderer = MKPolylineRenderer(overlay: overlay)
            renderer.strokeColor = .blue
            renderer.lineWidth = 4
            return renderer
        }
    }

    func makeCoordinator() -> Coordinator {
        Coordinator(self)
    }

    func makeUIView(context: Context) -> MKMapView {
        return MKMapView()
    }

    func updateUIView(_ mapView: MKMapView, context: Context) {
        var coordinates = self.directions.map({(location: CLLocation!) -> CLLocationCoordinate2D in return location.coordinate})
        let polyline = MKPolyline(coordinates: &coordinates, count: self.directions.count)

        mapView.delegate = context.coordinator
        mapView.addOverlay(polyline)
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-05
    • 2010-12-02
    • 2021-02-26
    • 2013-11-15
    相关资源
    最近更新 更多