Getting the User's Location
Receive location updates with CLLocationManager.
Starting Location Updates
Once authorized, call startUpdatingLocation() to begin receiving a continuous stream of fixes. The manager delivers them through the delegate. Always call stopUpdatingLocation() when you no longer need them to save battery.
manager.startUpdatingLocation()
// ... later
manager.stopUpdatingLocation()Receiving Locations in the Delegate
New fixes arrive in locationManager(_:didUpdateLocations:). The array is ordered oldest-to-newest, so the last element is the most recent location.
func locationManager(_ m: CLLocationManager,
didUpdateLocations locations: [CLLocation]) {
guard let latest = locations.last else { return }
print(latest.coordinate.latitude, latest.coordinate.longitude)
}All lessons in this course
- Requesting Location Authorization
- Getting the User's Location
- Displaying Maps in SwiftUI
- Geocoding and Regions