Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

247
Views
How to properly get user coordinates?

I make a MapViewController adopt the CLLocationManagerDelegate protocol this way:

extension MapViewController:CLLocationManagerDelegate{

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

        let usrLocation:CLLocation = locations[0] as CLLocation

        // readable
        print("lat = \(usrLocation.coordinate.latitude)")
        print("lon = \(usrLocation.coordinate.longitude)")

        self.userLocation = usrLocation

        locationManager.stopUpdatingLocation()

    }

    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error)
    {
        print("Error \(error)")
    }

    func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {

        switch status {
        case .restricted, .denied, .notDetermined:

            // no issue here because I make a very generic request to a server
            // AND I don't need user coordinates

            break

        case .authorizedWhenInUse, .authorizedAlways:

            // here I need user coordinates because I need to request content to a server
            // based on user coordinates but when I check for userLocation
            // at first I find <+0.00000000,+0.00000000> +/- 0.00m (speed -1.00 mps / course -1.00

            print("----------------")
            print(userLocation)
            print("----------------")

            break
        }
    }

I need user location data in the body of the .authorizedWhenInUse/.autorizeAlways case because I need to send current user coordinates to a server. Problem is when I need them I get at first a user location like this one:

 <+0.00000000,+0.00000000> +/- 0.00m (speed -1.00 mps / course -1.00>

userLocation is member of the viewcontroller class and its value is set in locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]). Should I move the request to this function? And how do I check the user status in that context? I only want to make requests for users who authorized the app to retrieve their position. I hope the solution is not a timer and multiple checks, but any help is appreciated :-)

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Just because you’ve received .authorizedWhenInUse or .authorizedAlways doesn’t mean that you’ve started location services yet. It just means that it has been authorized. You shouldn’t try to retrieve the user location in didChangeAuthorization.

So, you should call startUpdatingLocation and move your code that sends this location to the server to your didUpdateLocations method.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!