Geocoding and Regions
Convert coordinates to addresses and monitor regions.
Geocoding and Regions is a free Swift Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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:).
Frequently asked questions
Is the “Geocoding and Regions” lesson free?
Yes — the full text of “Geocoding and Regions” is free to read here on the web, and the Swift Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Swift Academy course, upgrade to CoddyKit PRO.
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; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
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.