Learn Courses My Dashboard

Updated "Realtime" showing current distance to a point

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.

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)

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() {
        manager.delegate = self
        manager.desiredAccuracy = kCLLocationAccuracyBest
    func 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:
        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:
    func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {

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 {
            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 {