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 :-)