0

I want to retrieve the location of the user by getting their coordinates. I tried retrieving it via CLLocationManager

    class LocationManager: NSObject, ObservableObject {
    private let locationManager = CLLocationManager()
    @Published var location: CLLocation?
    @Published var longtitude: CLLocationDegrees?
    @Published var latitude: CLLocationDegrees?
    
    override init() {
        super.init()
        
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.distanceFilter = kCLDistanceFilterNone
        locationManager.requestWhenInUseAuthorization()
        locationManager.startUpdatingHeading()
        locationManager.delegate = self
    }
}

extension LocationManager: CLLocationManagerDelegate {
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        guard let location = locations.last
            else { return }
        DispatchQueue.main.async {
            self.location = location
            self.longtitude = location.coordinate.longitude
            self.latitude = location.coordinate.latitude
        }
    }
}

The last few lines in the extension were what I was having trouble with. The results I got for location.coordinate.longitude was nil, and the same thing happens for latitude when I print it out. I need these two values, so I could place it as my MKPlacement in MapKit

Keep in mind that I updated my info.plist appropriately for privacy authorization.

Any help would be greatly appreciated :)

ios coder
  • 1
  • 4
  • 31
  • 91
Kelvin Jou
  • 271
  • 2
  • 12
  • did you set location of the simulator? – Ahmad Apr 02 '21 at 19:39
  • Not sure if I did it or not, actually. I was able to get UserLocation before(presenting it in a map), but could not get the coordinates. It would be helpful if you could tell me how to set location. – Kelvin Jou Apr 02 '21 at 19:41
  • https://stackoverflow.com/questions/214416/set-the-location-in-iphone-simulator you can set location like this – Ahmad Apr 02 '21 at 19:44
  • Oh yes, I used Paul Hudson's Control Room app for location mocking – Kelvin Jou Apr 02 '21 at 19:51
  • You have asked core location to start updating heading, not location, but that aside, `location.coordinate.longitude` can't be `nil`; it isn't an optional. Do you mean that it is `0`? – Paulw11 Apr 02 '21 at 20:10
  • @Paulw11 when I printed out `locationManager.latitude` and `locationManager.longitude` I receieved 2 `nil`s in the console. Yeah, I thought that it wasn't an optional, but it's giving me warnings to provide a default value or force unwrap – Kelvin Jou Apr 02 '21 at 20:23
  • Where did you print that? At what point in your code? Also, we my previous comment ; you are requesting heading updates, not location updates. – Paulw11 Apr 02 '21 at 20:29
  • How do I fix this? Thank you I didn't notice that it was also `nil`. – Kelvin Jou Apr 02 '21 at 20:29
  • I printed it in my actual `ContentView`, under ` .onAppear { locationManager.$location.sink { location in region = MKCoordinateRegion(center: location?.coordinate ?? CLLocationCoordinate2D(), latitudinalMeters: 500, longitudinalMeters: 500) print(location) } ` – Kelvin Jou Apr 02 '21 at 20:30
  • Okay I changed it to `startUpdatingLocation()` – Kelvin Jou Apr 02 '21 at 20:33
  • If you are receiving location updates then the location may indeed, initially, be `nil` since the first location update can take some time to arrive. This will be updated once a new location value is published. – Paulw11 Apr 02 '21 at 20:58
  • Oh I think I got it now, Thanks! – Kelvin Jou Apr 02 '21 at 21:05

0 Answers0