SwiftUI Maps & Location: Fundamentals

Mar 15 2022 · Swift 5.5, iOS 15, Xcode 13

Part 2: Core Location

13. Understand Core Location Components

Episode complete

Play next episode

Next
About this episode

Leave a rating/review

See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 12. Introduction Next episode: 14. Request Permission

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Notes: 13. Understand Core Location Components

The student materials have been reviewed and are updated as of January, 2022.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

Being a framework, Core Location contains a variety of components use to work with it. You’ve already encountered a few of them. For instance, you’ve already worked with the CLLocation and the CLLocationCoordinate2D.

CLLocation

The CLLocation contains the geographical location and altitude along with the accuracy of the measurements. It also contains the speed and heading of device in motion. Of course, CL in the CLLocation stands for Core Location so a CLLocation is a Core Location Location. Say that ten times fast.

CLLocationManager

But how do you get a user’s location? For that, you use the CLLocationManager. This object track both large and small changes with a configurable degree of accuracy. It will report heading changes. It will monitor regions and report the range of beacons. Note, we won’t be covering beacons in this course.

Activity Type

The activityType property lets Core Location know how you are using it. For instance, setting it to Fitness lets CoreLocation know the app is doing something like running or cycling, and may pause updates if there is no movement for a significant period time. There are a couple of ways to receive locations. You can either request a single location update or you can receive a constant stream of updates.

CLError

CLError contains lots and lots of different error conditions. A common error is LocationUnknown. This means the device is unable to get the current location. You may encounter this error when Core Location starts up. This is this is the best kind of error in that you may not have to do anything although if continue to receive this error after a period of time, something else may be at play. Let’s play around with locations.

LocationManager.swift

In this demo, we’re going to create our own location manager. We’ll create an object that essentially encapsulates the CLLocationManager. Open up your sample project and press command n to create a new Swift class. Call it LocationManager and save it in the model folder. First import the core Location framework.

import CoreLocation
final class LocationManager: ObservableObject  {

}
var locationManager = CLLocationManager()
  init() {
    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
  }
extension LocationManager: CLLocationManagerDelegate {

}
class LocationManager: NSObject, ObservableObject  {
override init() {
    super.init()
  func startLocationServices() {
    if locationManager.authorizationStatus == .authorizedAlways || locationManager.authorizationStatus == .authorizedWhenInUse {
      locationManager.startUpdatingLocation()
    } else {
      locationManager.requestWhenInUseAuthorization()
    }
  }
  func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
    if locationManager.authorizationStatus == .authorizedAlways || locationManager.authorizationStatus == .authorizedWhenInUse {
      locationManager.startUpdatingLocation()
    }
  }
@Published var locationString = ""
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    guard let latest = locations.first else { return }
    locationString = "location: \(latest.description)"
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {

}
guard let clError = error as? CLError else { return }
switch clError {
    case CLError.denied:
      print("Access denied")
    default:
      print("Catch all error")
    }
@StateObject var locationManager = LocationManager()
VStack {

}
Spacer()
Text(locationManager.locationString)
Spacer()
Button {
    locationManager.startLocationServices()
} label: {
    Text("Start Location Services")
}