Geocoding and Regions
Convert coordinates to addresses and monitor regions.
Geocoding and Regions is a free Swift Academy lesson on CoddyKit. This is lesson 4 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, and your progress syncs across the web and the CoddyKit app. The Swift Academy course includes 4 lessons in total.
What Is Geocoding?
Geocoding converts a human address into coordinates; reverse geocoding turns coordinates back into a readable address. Core Location provides CLGeocoder for both, talking to Apple's servers.
import CoreLocation
let geocoder = CLGeocoder()Forward Geocoding an Address
Call geocodeAddressString(_:) with a free-form address. The completion returns an array of CLPlacemark objects; take the first and read its location.
geocoder.geocodeAddressString("Eiffel Tower, Paris") { placemarks, error in
if let coord = placemarks?.first?.location?.coordinate {
print(coord.latitude, coord.longitude)
}
}Reverse Geocoding Coordinates
Given a CLLocation, reverseGeocodeLocation(_:) returns placemarks describing that point: street, city, country, postal code, and more.
let loc = CLLocation(latitude: 48.8584, longitude: 2.2945)
geocoder.reverseGeocodeLocation(loc) { placemarks, error in
if let p = placemarks?.first {
print(p.name ?? "", p.locality ?? "", p.country ?? "")
}
}The CLPlacemark Structure
A CLPlacemark exposes rich fields: name, thoroughfare (street), locality (city), administrativeArea (state), postalCode, country, and isoCountryCode. Many are optional, so unwrap carefully.
func format(_ p: CLPlacemark) -> String {
[p.thoroughfare, p.locality, p.country]
.compactMap { $0 }
.joined(separator: ", ")
}Geocoder Limitations
CLGeocoder is network-based and rate-limited. Issue one request at a time, avoid tight loops, and cancel in-flight work with cancelGeocode() when the result is no longer needed.
geocoder.cancelGeocode()Async/Await Geocoding
Modern Swift offers async variants, letting you write linear code with try await instead of completion handlers.
func coordinate(for address: String) async throws -> CLLocationCoordinate2D? {
let placemarks = try await geocoder.geocodeAddressString(address)
return placemarks.first?.location?.coordinate
}Introducing Region Monitoring
Region monitoring (geofencing) lets the system notify your app when the device enters or exits a defined circular area — even when your app is not running. It requires Always authorization.
// Requires NSLocationAlwaysAndWhenInUseUsageDescription
manager.requestAlwaysAuthorization()Defining a CLCircularRegion
A geofence is a CLCircularRegion with a center, radius (meters), and a unique identifier. Set notifyOnEntry and notifyOnExit to choose which events you care about.
let region = CLCircularRegion(
center: CLLocationCoordinate2D(latitude: 41.0, longitude: 29.0),
radius: 200,
identifier: "OfficeZone"
)
region.notifyOnEntry = true
region.notifyOnExit = trueStarting Region Monitoring
Call startMonitoring(for:) to begin. The device imposes a limit (around 20 regions per app), so monitor only the most relevant ones at a time.
if CLLocationManager.isMonitoringAvailable(for: CLCircularRegion.self) {
manager.startMonitoring(for: region)
}Receiving Enter/Exit Events
The delegate methods didEnterRegion and didExitRegion fire when boundaries are crossed. Use the region's identifier to know which geofence triggered.
func locationManager(_ m: CLLocationManager, didEnterRegion region: CLRegion) {
print("Entered:", region.identifier)
}
func locationManager(_ m: CLLocationManager, didExitRegion region: CLRegion) {
print("Exited:", region.identifier)
}Querying Current State
To learn whether the device is already inside a region without waiting for a crossing, call requestState(for:). The result arrives in didDetermineState as .inside, .outside, or .unknown.
manager.requestState(for: region)
func locationManager(_ m: CLLocationManager,
didDetermineState state: CLRegionState,
for region: CLRegion) {
print(region.identifier, state == .inside ? "inside" : "outside")
}Quick Check: Geocoding and Regions
Verify your understanding.
Recap: Geocoding and Regions
You learned to translate between addresses and coordinates and to watch geographic boundaries:
CLGeocoderdoes forward and reverse geocoding (async variants exist); it is rate-limited.CLPlacemarkholds rich address fields, all optional.CLCircularRegiondefines geofences; monitoring needs Always authorization.- Handle
didEnterRegion/didExitRegionand query state withrequestState(for:).
Learn Swift with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 122
- Lessons
- 409
Frequently Asked Questions
Is the “Geocoding and Regions” lesson free?
Yes — any 3 lessons of the Swift Academy learning path, including “Geocoding and Regions”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Swift Academy course includes 4 lessons in total.
What will I learn in “Geocoding and Regions”?
Convert coordinates to addresses and monitor regions. You practise Swift Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Swift Academy?
No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 4 of 4.
How long does the “Geocoding and Regions” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Swift Academy lesson?
Yes. Every Swift Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Requesting Location Authorization
- Getting the User's Location
- Displaying Maps in SwiftUI
- Geocoding and Regions