关于ios:如何在SwiftUI中获取MKMapView Directions

How to get MKMapView Directions in SwiftUI

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

这是我的内容视图:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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")
            }
        }
    }
}

这是我的地图视图:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
        }

    }
}

谢谢。


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

这样你的 updateUIView 将被更新的值相应地调用。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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)
    }
}