Updated "Realtime" showing current distance to a point

Hi
Being new to SwiftUI, tying to get my head around “Current Location”

What i want to do: I want to within a view to calculate the distance to a specific fixed point.
C2

Using the instruction provided by “CodeWithCris” and more
i have been able to create a map, showing my current location (Not needing that in the end)
I have also been able to get a printout of Latitude and Longitude

The problem is the following

CLLocation contains the function distance

And if I create the code below with 2 static locations (place1 and place2 ) it works

let place1 = CLLocation(latitude: 55.000, longitude: 12.000)
let place2 = CLLocation(latitude: 55.890070, longitude: 12.445504)
var distanceInMeters:Double = place1.distance(from: place2)
Text(String(distanceInMeters))

My problem starts when I try to make place1 reflecting my current location. I can not get my head around how to do this.

My current code is:


import SwiftUI
import CoreLocation
import CoreLocationUI
import MapKit


enum MapDetails {
    static let startingLocation = CLLocationCoordinate2D(latitude: 55.677984489004544, longitude: 12.524586647705545)
    static let defaultSpan = MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01)
}

class MapViewModel: NSObject, ObservableObject, CLLocationManagerDelegate {
    
    @Published var region = MKCoordinateRegion(center: MapDetails.startingLocation, span: MapDetails.defaultSpan)
    var locationManager: CLLocationManager?
    private let manager = CLLocationManager()
    @Published var location: CLLocationCoordinate2D?
    
    func checkIfLocationServicesIsEnabled() {
        if CLLocationManager.locationServicesEnabled() {
            locationManager = CLLocationManager()
            locationManager!.delegate = self
            
        } else {
            print("You are fucked")
        }
    }
    
    override init() {
        super.init()
        manager.delegate = self
        manager.desiredAccuracy = kCLLocationAccuracyBest
        manager.requestWhenInUseAuthorization()
        manager.startUpdatingLocation()
    }
    
    func requestLocation() {
        manager.requestLocation()
    }
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        locations.last.map { _ in region = MKCoordinateRegion(center: MapDetails.startingLocation, span: MapDetails.defaultSpan)}
    }
    
    
    private func checkLocationAutorization() {
        guard let locationManager = locationManager else { return }
        
        switch locationManager.authorizationStatus {
            
        case .notDetermined:
            locationManager.requestWhenInUseAuthorization()
        case .restricted:
            print("Your location is restricted")
        case .denied:
            print("You have denied this app location permission")
        case .authorizedAlways, .authorizedWhenInUse:
            region = MKCoordinateRegion(center: locationManager.location!.coordinate,
                                        span: MapDetails.defaultSpan)
        @unknown default:
            break
        }
        
    }
    func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
        checkLocationAutorization()
        
        
    }
    
    
}

struct MapInfoView: View {
    @StateObject var locationManager = MapViewModel()
    @State var tracking:MapUserTrackingMode = .follow
    
    var body: some View {
        VStack {
            Map(coordinateRegion: $locationManager.region , interactionModes: MapInteractionModes.all,   showsUserLocation: true, userTrackingMode: $tracking)
                .frame(maxHeight: 200)
                .onAppear {
                    locationManager.checkIfLocationServicesIsEnabled()
                    
                }
            
            switch locationManager.locationManager?.authorizationStatus {
            case .authorizedWhenInUse, .some(.authorizedAlways):
                Text("Your current location is:")
                Text("Latitude: \(locationManager.locationManager?.location?.coordinate.latitude.description ?? "Error loading")")
                Text("Longitude: \(locationManager.locationManager?.location?.coordinate.longitude.description ?? "Error loading")")
                //               var place1 = CLLocation(latitude: locationManager.location?.latitude, longitude: locationManager.location?.longitude)
                //                let place2 = CLLocation(latitude: 55.890070, longitude: 12.445504)
                //                @State var distanceInMeters:Double = place1.distance(from: place2)
                //                Text(String(distanceInMeters))
               
            case .restricted, .denied, .none, .some(.notDetermined), .some(_):
                Text("Current location data was restricted or unavailable.")
            }
        }
        
    }
    
}

struct MapInfoView_Previews: PreviewProvider {
    static var previews: some View {
        MapInfoView()
    }
}

The following solution is working for me:

In the main view:

switch locationManager.locationManager?.authorizationStatus {
                            
                        case .authorizedWhenInUse, .some(.authorizedAlways):
                            
                            @State var place1 = CLLocation(latitude: locationManager.currentLocation?.latitude ?? 0.00, longitude: locationManager.currentLocation?.longitude ?? 0.00)
                            let place2 = CLLocation(latitude: info.Lat, longitude: info.Lon)
                            @State var distanceInMeters:Double = place1.distance(from: place2)
                            Text("Afstand til midten af green:")
                                .padding(.bottom)
                            
                            Text("\(distanceInMeters, specifier: "%.2f")")
                                .font(.title)
                            
                        case .restricted, .denied, .none, .some(.notDetermined), .some(_):
                            Text("Current location data was restricted or unavailable.")
                                .font(.title)

This uses a information in MapFunctions:

import SwiftUI
import CoreLocation
import CoreLocationUI
import MapKit
import Foundation



enum MapDetails {
    static let startingLocation = CLLocationCoordinate2D(latitude: 55.677984489004544, longitude: 12.524586647705545)
    static let defaultSpan = MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01)
}

final class MapViewModel: NSObject, ObservableObject, CLLocationManagerDelegate {
    
    @Published var region = MKCoordinateRegion(center: MapDetails.startingLocation, span: MapDetails.defaultSpan)
    var locationManager: CLLocationManager?
    private let manager = CLLocationManager()
    @Published var location: CLLocationCoordinate2D?
    @Published var currentLocation: CLLocationCoordinate2D?
    
    
    func checkIfLocationServicesIsEnabled() {
        if CLLocationManager.locationServicesEnabled() {
            locationManager = CLLocationManager()
            locationManager!.delegate = self
            
        } else {
            print("You are fucked")
        }
    }
    
    override init() {
        super.init()
        manager.delegate = self
        manager.desiredAccuracy = kCLLocationAccuracyBestForNavigation
        manager.requestWhenInUseAuthorization()
        manager.startUpdatingLocation()
        
    }
    
    func requestLocation() {
        manager.requestLocation()
    }
    
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        
        if let location = locations.last {
            currentLocation = CLLocationCoordinate2D(
                latitude: location.coordinate.latitude,
                longitude: location.coordinate.longitude)
            
        }
    }
    
    
    
    private func checkLocationAutorization() {
        guard let locationManager = locationManager else { return }
        
        switch locationManager.authorizationStatus {
            
        case .notDetermined:
            locationManager.requestWhenInUseAuthorization()
        case .restricted:
            print("Your location is restricted")
        case .denied:
            print("You have denied this app location permission")
        case .authorizedAlways, .authorizedWhenInUse:
            region = MKCoordinateRegion(center: locationManager.location!.coordinate,
                                        span: MapDetails.defaultSpan)
            
        @unknown default:
            break
        }
        
    }
    func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
        checkLocationAutorization()
        
        
    }
    
    
}