ContentView 中的按钮缩放到 MapBox 地图上的当前位置?

Button in ContentView that zooms to current location on MapBox map?

所以....在我的协调器中——它符合 mapbox 委托协议,我可以这样做:

mapView.setCenter(mapView.userLocation!.coordinate, zoomLevel: 13, animated: true)

并且此函数在协调器中或在 mapView class 中调用时工作正常。唯一的问题是我不知道如何传递这个 mapView 实例(特别是返回到 ContentView 中,我想要一个按钮来做同样的事情)。我也有一个 LocationManager 结构,但我不知道它在这里有多少用处。传递 MapView 实例是完成我想做的事情的最简单方法吗?

提前致谢!

这里有一个解决方案的demo(基于MapKit,但对idea不重要)。测试 Xcode 12.

struct DemoActionToMapView: View {
    @State private var centerToUser: () -> () = {}
    var body: some View {
        VStack {
            Button("Center", action: centerToUser)
            MapView { map in
                self.centerToUser = {
                    map.setCenter(map.userLocation.coordinate, animated: true)
                }
            }
        }
    }
}

struct MapView: UIViewRepresentable {
    var configure: (MKMapView) -> () = { _ in }

    func makeUIView(context: Context) -> MKMapView {
        let map = MKMapView()
        DispatchQueue.main.async {
            configure(map)
        }
        return map
    }

    func updateUIView(_ uiView: MKMapView, context: Context) {
    }
}