У меня есть этот простой 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)")
}
}
В этом случае карта не обновляется.
Если я добавлю логику в ContentView без ObservedObject, как это:
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]
}
}
тогда карта обновляется.
Не могли бы вы помочь мне с этим?