如何通过Binding和ObservedObject更新UIViewRepresentable地图

时间:2019-11-06 08:46:19

标签: binding mapkit swiftui combine

我有一个简单的MapView,并且有一个ObservedObject数据源要更新,但是不起作用。

这是内容视图:

struct ContentView: View {
    @ObservedObject var trackingOnMapViewModel = TrackingOnMapViewModel()

    var body: some View {
        ZStack {
            MapView(selectedRegion: $trackingOnMapViewModel.selectedRegion)
                .edgesIgnoringSafeArea(.vertical)
            VStack {
                Spacer()
                Button(action: {
                    self.trackingOnMapViewModel.selectNextRegion()
                }) {
                    Text("Next")
                        .padding()
                }
            }
        }
    }
}

这是一个简单的地图视图:

struct MapView: UIViewRepresentable {
    @Binding var selectedRegion: MKCoordinateRegion

    func makeUIView(context: Context) -> MKMapView {
        print("MapView - makeUIView")
        let map = MKMapView()
        return map
    }

    func updateUIView(_ mapView: MKMapView, context: Context) {
        print("MapView - updateUIView")
        mapView.setRegion(selectedRegion, animated: true)
    }
}

这是数据源:

class TrackingOnMapViewModel: ObservableObject {

    var regions: [MKCoordinateRegion] = [
        MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 10.0, longitude: 10.0), span: MKCoordinateSpan(latitudeDelta: 2.0, longitudeDelta: 2.0)),
        MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 20.0, longitude: 20.0), span: MKCoordinateSpan(latitudeDelta: 2.0, longitudeDelta: 2.0)),
        MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 30.0, longitude: 30.0), span: MKCoordinateSpan(latitudeDelta: 2.0, longitudeDelta: 2.0)),
        MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 40.0, longitude: 40.0), span: MKCoordinateSpan(latitudeDelta: 2.0, longitudeDelta: 2.0))
    ]

    var selectedRegion: MKCoordinateRegion = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 0.0, longitude: 0.0), span: MKCoordinateSpan(latitudeDelta: 2.0, longitudeDelta: 2.0))

    var currentIndex = 0


    func selectNextRegion() {
        print("TrackingOnMapViewModel - selectNextLandmark")
        currentIndex = currentIndex < regions.count-1 ? currentIndex + 1 : 0
        self.selectedRegion = regions[currentIndex]
        print("selectedRegion - \(selectedRegion)")
    }
}

在这种情况下,地图不会更新。

如果我在没有ObservedObject的情况下将逻辑放入ContentView:

struct ContentView: View {
//    @ObservedObject var trackingOnMapViewModel = TrackingOnMapViewModel()

@State var regions: [MKCoordinateRegion] = [
    MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 10.0, longitude: 10.0), span: MKCoordinateSpan(latitudeDelta: 2.0, longitudeDelta: 2.0)),
    MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 20.0, longitude: 20.0), span: MKCoordinateSpan(latitudeDelta: 2.0, longitudeDelta: 2.0)),
    MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 30.0, longitude: 30.0), span: MKCoordinateSpan(latitudeDelta: 2.0, longitudeDelta: 2.0)),
    MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 40.0, longitude: 40.0), span: MKCoordinateSpan(latitudeDelta: 2.0, longitudeDelta: 2.0))
]

@State var selectedRegion: MKCoordinateRegion = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 0.0, longitude: 0.0), span: MKCoordinateSpan(latitudeDelta: 2.0, longitudeDelta: 2.0))

@State var currentIndex = 0

var body: some View {
    ZStack {
        MapView(selectedRegion: $selectedRegion)
            .edgesIgnoringSafeArea(.vertical)
        VStack {
            Spacer()
            Button(action: {
                self.selectNextRegion()
            }) {
                Text("Next")
                    .padding()
            }
        }
    }
}

private func selectNextRegion() {
    print("ContentView - selectNextLandmark")
    currentIndex = currentIndex < regions.count-1 ? currentIndex + 1 : 0
    self.selectedRegion = regions[currentIndex]
}

}

然后地图正在更新。

您能帮我吗?

1 个答案:

答案 0 :(得分:0)

当我发布问题时,我发现了问题。 我忘了在TrackingOnMapViewModel中将selectedRegion变量标记为@Published。

@Published var selectedRegion: MKCoordinateRegion = MKCoordinateRegion