Sitemap
Harsha Agarwal

Harsha Agarwal

iOS dev sharing my learning journey. I use Medium to stay accountable as I explore Apple's ecosystem. Expect real-world analogies and celebrity website mockups.

Understanding CoreLocation in Swift

4 min readJul 9, 2025

--

Press enter or click to view image in full size

You know that little screen on DoorDash that shows your food moving toward you? Yeah, I stare at it like it’s a Netflix show. It got me curious: How does that even work? That led me into the world of maps and, more specifically, CoreLocation in Swift. And let me tell you, at first, it felt like I was reading ancient code. So here’s my attempt to break it down in plain English.

What Is CoreLocation?

CoreLocation is a built-in framework in Swift that helps you find out where a device is. Whether you want to show the user’s location on a map, track how far they’ve walked, or figure out which city they’re in, CoreLocation is what you use.

Real-Life Analogy: The Human GPS

Let’s say you’re meeting a friend at a concert. You call them up and ask, “Where are you right now?” They reply, “I’m near the ticket counter, next to the food truck.” That’s location tracking in human terms. CoreLocation lets your app do something similar.

It can:

  • Tell you the user’s current location
  • Monitor when they enter or leave an area (geofencing)
  • Track their movement over time (significant location changes)

Step-by-Step: Setting It Up

Here’s a super simple example to get started.

1. Import the Framework

import CoreLocation

2. Ask for Permission

Before you can access the user’s location, you must ask for their permission.

In Info.plist, add:

<key>NSLocationWhenInUseUsageDescription</key>
<string>We need your location to show it on the map</string>

3. The Code:

class LocationManager: NSObject, ObservableObject, CLLocationManagerDelegate {
private let manager = CLLocationManager()
@Published var userLocation: CLLocationCoordinate2D?

override init() {
super.init()
manager.delegate = self
manager.requestWhenInUseAuthorization()
manager.startUpdatingLocation()
}

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let location = locations.last {
DispatchQueue.main.async {
self.userLocation = location.coordinate
print("User location: \(location.coordinate.latitude), \(location.coordinate.longitude)")
}
}
}
}

This example is also SwiftUI-ready! You can observe userLocation to update your UI in real-time.

SwiftUI Bonus: Show Location on a Map

If you’re building a SwiftUI app, here’s how you can show the user’s location on a map:

import MapKit
import SwiftUI

struct LocationView: View {
@StateObject private var locationManager = LocationManager()

var body: some View {
if let location = locationManager.userLocation {
Map(
coordinateRegion: .constant(
MKCoordinateRegion(
center: location,
span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01)
)
),
showsUserLocation: true
)
} else {
Text("Fetching location...")
}
}
}

This renders a live map with the user’s position updated using CoreLocation.

Real-World Example: The DoorDash Map

When I was curious about DoorDash’s map feature, I realized it constantly checks two things:

  1. Where you are.
  2. Where the dasher is.

CoreLocation handles the first part. Combined with a backend and a mapping SDK like MapKit, that live tracking effect is created.

Get Harsha Agarwal’s stories in your inbox

Join Medium for free to get updates from this writer.

So next time you see your food move closer, just know there’s some CLLocationManager working in the background.

Special Cases and Edge Cases

1. Permissions Denied

If the user says “No” to location access, CoreLocation won’t work. Always check for the latest status using the instance method:

if manager.authorizationStatus == .denied {
print("Location access denied. Show an alert or guide user to settings.")
}

2. Simulator Woes

If you’re testing in the iOS Simulator, you’ll need to manually set a location from the Debug > Location menu. Otherwise, your coordinates will return as 0.0, 0.0.

3. Battery Usage

Location tracking can drain battery. Use .startMonitoringSignificantLocationChanges() instead of .startUpdatingLocation() if you don't need high accuracy.

When to Use CoreLocation vs MapKit

Before we wrap up, here’s a quick guide on when to use CoreLocation and when to bring in MapKit because while they often work together, they solve different problems.

Use CoreLocation when:

  • You want to get the user’s current location (latitude and longitude).
  • You need to track location changes (e.g., walking, driving).
  • You want to set up geofences or react to entering/leaving specific regions.
  • You don’t need a visual map but just the coordinates.

Use MapKit when:

  • You want to display maps (standard, satellite, hybrid views).
  • You want to add pins or annotations to a map.
  • You want to draw routes or directions between two places.
  • You want to create a map experience like what you see in DoorDash or Uber.

Use Both Together When:

  • You want to track user location and show it live on a map.
  • You’re building apps like DoorDash, Uber, or any delivery tracker.

Think of it this way:

CoreLocation tells you where you are. MapKit shows you where that is.

CoreLocation might sound intimidating at first, but it’s like asking your phone, “Where are you?” and getting an honest answer.

If you’re dreaming of building the next DoorDash, Uber, or fitness tracker, this is where you start. It’s simple, powerful, and essential.

You can dive deeper into the official Apple documentation here.

Hope this helped you get a grip on the basics of CoreLocation. And if you ever build an app that lets me track my coffee in real time, I’m downloading it.

Happy coding!

--

Harsha Agarwal
Harsha Agarwal

Written by Harsha Agarwal

iOS dev sharing my learning journey. I use Medium to stay accountable as I explore Apple's ecosystem. Expect real-world analogies and celebrity website mockups.