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 { Button(action: { |
这是我的地图视图:
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 { func updateUIView(_ uiView: MKMapView, context: Context) { func makeCoordinator() -> Coordinator { func getDirections() { class Coordinator: NSObject, MKMapViewDelegate { func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer { } |
谢谢。
- 你收到什么错误?为什么不能调用那个函数?我们需要更多信息来提供帮助。
- 我已经更新了问题。我觉得比较好理解。谢谢。
- 我的回答有帮助吗?
在您的 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 { Button(action: { struct MapView: UIViewRepresentable { class Coordinator: NSObject, MKMapViewDelegate { init(_ parent: MapView) { func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer { func makeCoordinator() -> Coordinator { func makeUIView(context: Context) -> MKMapView { func updateUIView(_ mapView: MKMapView, context: Context) { mapView.delegate = context.coordinator |
来源:https://www.codenong.com/59182750/