# SwiftUI Maps & Location: Fundamentals

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

## Part 2: Core Location

### Episode complete

Play next episode

Next

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.

#### Notes: 15. Challenge: Determine Distance

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

Determining the distance between two places is something that Core Location provides right out of the box. You just need two things. You need a start location and then you need an end location. And that’s what you’ll be doing in this challenge. Your challenge is calculate the distance from your current location to one of the interesting places. Print out the distance on the location distance UILabel.

## Challenge

To do this, you’ll need to get the start location of the user and then when they tap on a location, you’ll need to get the distance from that location. You’ll do this using the distance method on CLLocation. This returns the distance in meters between another location. Once you have your location, you can either use it or do a simple conversion.

## Solution

How’d that challenge go for you. If you got stuck, don’t worry about it. Learning an APi takes both time and practice and getting stuck is part of the process. Okay, so open your project from when you last worked on it. Open up the location manager.

``````var previousLocation: CLLocation?
``````
``````if previousLocation == nil {
previousLocation = latest
}
``````
``````else {
let distanceInMeters = previousLocation?.distance(from: latest) ?? 0
}
``````
``````previousLocation = latest
``````
``````locationString = "You are \(Int(distanceInMeters)) meters from your start point."
``````