如何在 SwiftUI 中跟踪 MapView 中心的纬度和经度?

How to track the Latitude and Longitude at the center of a MapView in SwiftUI?

我想创建一个 MapView,它跟踪 MapView 中心的经度和纬度,并将其存储在两个状态变量中。如何以这种方式跟踪 latitude/longitude?

我试过使用 region 来查看 center 是否随着地图移动而更新,但不幸的是它没有:

@State private var region: MKCoordinateRegion = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 40.75773, longitude: -73.985708), span: MKCoordinateSpan(latitudeDelta: 0.05, longitudeDelta: 0.05))
var body: some View {
        ZStack {
            Map(coordinateRegion: $viewModel.region,
                showsUserLocation: true,
                annotationItems: getNearbyEvents()
            ) {/* places annotations on the map */}
            }
                .edgesIgnoringSafeArea(.all)
                .accentColor(Color("BlueAccent"))
                .onAppear {
                    viewModel.checkLocationServicesEnabled()
                }
            // Displays region center as text... doesn't update with movement :(
            Text("Long: \(region.center.longitude) Lat: \(region.center.latitude)")
        }
    }

为地图分配区域,例如:

region = MKCoordinateRegion(center: location.coordinate, span: MKCoordinateSpan(latitudeDelta: self.zoomLevel, longitudeDelta: self.zoomLevel))

viewModel.regionText 中使用的 region 不同,请改用 viewModel.region

Text("Long: \(viewModel.region.center.longitude) Lat: \(viewModel.region.center.latitude)")