首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何通过绑定和UIViewRepresentable更新ObservedObject地图

如何通过绑定和UIViewRepresentable更新ObservedObject地图
EN

Stack Overflow用户
提问于 2019-11-06 08:46:19
回答 1查看 959关注 0票数 1

我有这个简单的MapView,我有一个要更新的ObservedObject数据源,但是没有工作。

这是内容视图:

代码语言:javascript
复制
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()
                }
            }
        }
    }
}

这是一个简单的地图:

代码语言:javascript
复制
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)
    }
}

这是数据来源:

代码语言:javascript
复制
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中,如下所示:

代码语言:javascript
复制
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]
}

}

然后地图正在更新。

你能帮我做这个吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-11-06 08:51:48

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

代码语言:javascript
复制
@Published var selectedRegion: MKCoordinateRegion = MKCoordinateRegion
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58726165

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档