SwiftUI - MKMapView - 在@EnvironmentObject 变量中显示地图中心坐标

SwiftUI - MKMapView - Display map center coordinate in a @EnvironmentObject variable

有人可以帮我解决我的问题吗?

我有一个 SwiftUI 项目并编写了一个 MKMapView 文件。我想将中心坐标存储在@EnvironmentObject 变量中。

不幸的是,我无法将中心坐标打包到 EnvironmentObject 变量中。

我的代码如下所示:

struct MapView: UIViewRepresentable {
    
    @EnvironmentObject var messpunkt_manager: Messpunkt_Manager
    
    
    func makeUIView(context: Context) -> MKMapView {
        
        let map = MKMapView()
        map.showsUserLocation = true // zeigt den aktuellen Standort des Users
        map.delegate = context.coordinator // ermöglich die Bedienung
       

        // shows annotations already in array
        for location in messpunkt_manager.messpunkte {
            let annotation = MKPointAnnotation()
            annotation.title = location.title
            annotation.coordinate = CLLocationCoordinate2D(latitude: location.latitude, longitude: location.longitude)
            map.addAnnotation(annotation)
            
        }
        
        return map
    }
    
    
    
    
    func updateUIView(_ uiView: MKMapView, context: UIViewRepresentableContext<MapView>) {
        
        // start coordinates
        let coordinate = CLLocationCoordinate2D(latitude: messpunkt_manager.centerCoordinate.latitude, longitude: messpunkt_manager.centerCoordinate.longitude)
        let span = MKCoordinateSpan(latitudeDelta: messpunkt_manager.centerZoom.latitudeDelta, longitudeDelta: messpunkt_manager.centerZoom.longitudeDelta)
        let region = MKCoordinateRegion(center: coordinate, span: span)
        
        uiView.setRegion(region, animated: true)

        
        // Adds annotation to the map
        for location in messpunkt_manager.messpunkte {
            let annotation = MKPointAnnotation()
            annotation.title = location.title
            annotation.coordinate = CLLocationCoordinate2D(latitude: location.latitude, longitude: location.longitude)
            uiView.addAnnotation(annotation)
        }
        
        
        
        if messpunkt_manager.karteAusgeaehlt == 0 {
            uiView.mapType = .standard
        }
        if messpunkt_manager.karteAusgeaehlt == 1 {
            uiView.mapType = .hybrid
        }
        if messpunkt_manager.karteAusgeaehlt == 2 {
            uiView.mapType = .satellite
        }
    }


    func makeCoordinator() -> Coordinator {
        Coordinator(self)
    }
    
}

然后应将中心坐标放入此@EnvironmentObject 变量

@Published var position = CLLocationCoordinate2D()

感谢您的帮助!

对不起我的命名。对我来说,用我的母语德语命名变量、函数……更容易。我希望这不是什么大问题。

在您的 class Coordinator 中您需要设置值。假设您的协调器设置如下:

class Coordinator: NSObject, MKMapViewDelegate {
        
    var parent: MapView
    
    init(_ parent: MapView) {
        self.parent = parent
    }
    ...
}

然后你会添加这个函数:

func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
    parent. messpunkt_manager.position = mapView.region
}

对post一个Minimal Reproducible Example (MRE)真的很有帮助。如您所见,您遗漏了一个重要的部分