Search code examples
swiftuimkmapview

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


can someone please help me with my problem?

I have a SwiftUI Project and wrote a MKMapView File. I would like to store the Center coordinates in a @EnvironmentObject variable.

Unfortunately, I can't manage to pack the center coordinates into the EnvironmentObject variable.

my code looks like this:

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)
    }
    
}

the center coordinates should then be put into this @EnvironmentObject variable

@Published var position = CLLocationCoordinate2D()

thanks for your help!

And sorry for my naming. For me its easier to name the variables, functions ... in my native language German. I hope its not a big deal.


Solution

  • In your class Coordinator you need to set the value. Assuming your coordinator is set up like this:

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

    Then you would add this function:

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

    It is really helpful to post a Minimal Reproducible Example (MRE). As you can see, you left out an important piece