Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

254
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda