0

I am trying to display user location coordinates on screen but nothing shows up when i run the app even though i can see coordinates in console. And the second problem i have is when i press the stop button the location updates do not stop.

LocationManager:

import Foundation
import CoreLocation

class LocationManager: NSObject, ObservableObject, CLLocationManagerDelegate{
    let locationManager = CLLocationManager()
    @Published var location: CLLocationCoordinate2D?
    
    override init(){
        super.init()
        locationManager.delegate = self
    }
    
    func startTracking() {
        locationManager.requestAlwaysAuthorization()
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.distanceFilter = kCLDistanceFilterNone
        locationManager.allowsBackgroundLocationUpdates = true
        locationManager.startUpdatingLocation()
    }
    
    func stopTracking() {
        print("stop test")
        locationManager.stopUpdatingLocation()
        locationManager.allowsBackgroundLocationUpdates = false
    }
    
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        let tempLocation = locations.last?.coordinate
        print(tempLocation)
        DispatchQueue.main.async {
            self.location = tempLocation
        }
    }
}

View:

import SwiftUI
struct ContentView: View {
    @StateObject var locationManager = LocationManager()
    
    var body: some View {
        VStack{
            startLocationTrackingButton()
            stopLocationTrackingButton()
            
            if let location = locationManager.location {
                Text("Your location: \(location.latitude), \(location.longitude)")
            }
            
        }
    }
}
struct startLocationTrackingButton: View{
    @StateObject var locationManager = LocationManager()
    
    var body: some View{
        Button("START"){
            locationManager.startTracking()
            
        }
    }
}
struct stopLocationTrackingButton: View{
    @StateObject var locationManager = LocationManager()
    
    var body: some View{
        Button("STOP"){
            locationManager.stopTracking()
        }
    }
}

Thanks for help!

1 Answer 1

1

Each of your views has:

@StateObject var locationManager = LocationManager()

You are creating a different Location manager for each one. Try doing it as:

truct ContentView: View {
    @StateObject var locationManager = LocationManager()
    
    var body: some View {
        VStack{
            StartLocationTrackingButton(manager: locationManager)
            StopLocationTrackingButton(manager: locationManager)
            
            if let location = locationManager.location {
                Text("Your location: \(location.latitude), \(location.longitude)")
            }
            
        }
    }
}
struct StartLocationTrackingButton: View{
    @ObservedObject var manager: LocationManager
    
    var body: some View{
        Button("START"){
            manager.startTracking()
            
        }
    }
}
struct stopLocationTrackingButton: View{
    @ObservedObject var manager: LocationManager
    
    var body: some View{
        Button("STOP"){
            manager.stopTracking()
        }
    }
}

Only use @StateObject when you are creating and owning the object. If you are passing on ObservableObject to another view, then that View should declare it as @ObservedObject and don't give it an initial value.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.