SwiftUI creating MapCameraPosition from CLLocationManager initialiser/self error when trying to tie them? (see code)

Trying to use new Swift @Observable to monitor GPS position within SwiftUI content view. But how do I tie the latest locations to the SwiftUI Map's mapCameraPosition?

Well ideally the answer could cover:

  1. How to fix this error - So get map tracking along with the User Position, but also
  2. How to include facility to turn on/off the map moving to track the user position (which I'll need to do next). So could be tracking, then disable, move map around and have a look at things, then click button to start syncing the mapcameraposition to the GPS location again

Refer to error I'm embedded in the code below.

import SwiftUI
import MapKit

@Observable
final class NewLocationManager : NSObject, CLLocationManagerDelegate {
    var location: CLLocation? = nil
    private let locationManager = CLLocationManager()

    func startCurrentLocationUpdates() async throws {
        if locationManager.authorizationStatus == .notDetermined {
            locationManager.requestWhenInUseAuthorization()
        }
        for try await locationUpdate in CLLocationUpdate.liveUpdates() {
            guard let location = locationUpdate.location else { return }
            self.location = location
        }
    }
   
}

struct ContentView: View {
    var newlocationManager = NewLocationManager()
    
    @State private var cameraPosition: MapCameraPosition = .region(MKCoordinateRegion(
        center: newlocationManager.location?.coordinate ?? <#default value#>,
        span: MKCoordinateSpan(latitudeDelta: 0.25, longitudeDelta: 0.25)
    ))
    // GET ERROR: Cannot use instance member 'newlocationManager' within property initializer; property initializers run before 'self' is available

    var body: some View {
        ZStack {
            Map(position: $cameraPosition)
            Text("New location manager: \(newlocationManager.location?.description ?? "NIL" )") // works
        }
        .task {
            try? await newlocationManager.startCurrentLocationUpdates()
        }
        
    }
}

#Preview {
    ContentView()
}

Replies

PS another way to ask this question might be - Within the view, how can I arrange for some code to be run each time a value within "newlocationManager" (which references the @Observable NewLocationManager instance) changes? (so within this code I can decided to update the Map position centering on this new location or not)