CFRJ
CFRJ

Reputation: 157

Update location when user allows location tracking

I'm trying to animate where the user is when they accepts location tracking. The Code works when they already have accepted location tracking and then loads the view but I would like to have the view reload when they press accept on location tracking.

override func viewDidLoad() {
    super.viewDidLoad()


    //Prepare to get User
     if CLLocationManager.authorizationStatus() != .authorizedAlways {
         // May need to change to support location based notifications
        locationManager.requestAlwaysAuthorization()
        print("hello")

        mapView.setVisibleMapRect(mapView.visibleMapRect, animated: true)
     }else {
        locationManager.startUpdatingLocation()
     }
    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyBest

    mapView.delegate = self

}

Animated to user location Code

//Get user location
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    //Show the map at the users current location
    let location = locations[0]
    let span:MKCoordinateSpan = MKCoordinateSpanMake(0.02,0.02 )
    let myLocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)
    let region:MKCoordinateRegion = MKCoordinateRegionMake(myLocation, span)
    mapView.setRegion(region, animated: true)
    self.mapView.showsUserLocation = true

    locationManager.stopUpdatingLocation()


 }

Upvotes: 0

Views: 192

Answers (2)

Vasil Hristov
Vasil Hristov

Reputation: 132

You can use:

func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
    switch status {
    case .notDetermined:
        locationManager.requestAlwaysAuthorization()
        break
    case .authorizedWhenInUse:
        locationManager.startUpdatingLocation()
        break
    case .authorizedAlways:
        locationManager.startUpdatingLocation()
        break
    default:
        break
    }
}

Upvotes: 2

David Pasztor
David Pasztor

Reputation: 54706

All methods used to update locations in CLLocationManager are asynchronous, so there might be a delay from the user allowing location access to the location update actually happening regardless of the implementation of your code.

However, by calling CLLocationManager().requestLocation() inside locationManager(didChangeAuthorizationStatus) you can ensure that you receive a location update as close as possible to the acceptance of location usage.

func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
    switch status {
    case .notDetermined:
        locationManager.requestAlwaysAuthorization()
    case .authorizedWhenInUse:
        locationManager.requestLocation()
    case .authorizedAlways:
        locationManager.requestLocation()
    default:
        break
    }
}

This will automatically call your CLLocationManagerDelegate method as soon as the asynchronous requestLocation function finishes execution.

Upvotes: 0

Related Questions