MapKit
Build map-based and location-aware features targeting iOS 17+ with SwiftUI
MapKit and modern CoreLocation async APIs. Use Map with MapContentBuilder
for views, CLLocationUpdate.liveUpdates() for streaming location, and
CLMonitor for geofencing.
Read references/mapkit-patterns.md when you need full map setup, search,
routes, Look Around, snapshots, or iOS 26 place APIs. Read
references/mapkit-corelocation-patterns.md when the task involves
location update lifecycle, geofencing, background location, testing, or privacy keys.
Contents
Workflow
- Define the exact map/location capability and request only the authorization it needs.
- Choose SwiftUI Map or UIKit hosting, then establish camera ownership, selection, annotations, and overlay identity.
- Keep search, geocoding, directions, and live-location tasks cancellable and scoped to the current query/session.
- Handle denied/restricted/reduced-accuracy states and avoid treating coordinates as always available.
- Verify empty results, movement, camera changes, route failures, localization, offline/network transitions, and background rules.
Route by Task
- Read core implementation details for SwiftUI maps, camera, annotations, overlays, live updates, authorization, geocoding, search, and directions.
- Read MapKit patterns for clustering, Look Around, snapshots, complex routes, and full map setup.
- Read Core Location patterns for
CLLocationUpdate, monitoring, service sessions, background behavior, and testing.
Core Decisions
- Do not infer authorization from API availability or a non-nil cached location.
- Use stable identities for annotations and overlays.
- Debounce and cancel search/geocoding work when input changes.
- Treat size, viewport, and user interaction as live state rather than fixed screen assumptions.
Common Mistakes
DON'T: Request always authorization upfront.
DO: Start with when-in-use authorization. On iOS 18+, hold a CLServiceSession
for the feature lifetime; request .always only for background features that
need system relaunch after termination.
DON'T: Use CLLocationManagerDelegate for simple location fetches on iOS 17+.
DO: Use CLLocationUpdate.liveUpdates() async stream for cleaner, more concise code.
DON'T: Ignore CLLocationUpdate diagnostics such as denied, globally denied, or unavailable location.
DO: Stop or degrade the feature, show recovery UI such as Settings guidance, and keep search/manual flows usable.
DON'T: Let liveUpdates() run from an unowned task after the map/view is gone.
DO: Store the Task, cancel it when the feature stops, and filter invalid, inaccurate, stale, or impossible movement fixes.
DON'T: Force-unwrap CLPlacemark properties — they are all optional.
DO: Use nil-coalescing: placemark.locality ?? "Unknown".
DON'T: Fire MKLocalSearchCompleter queries on every keystroke.
DO: Debounce with .task(id: searchText) + Task.sleep(for: .milliseconds(300)).
DON'T: Silently fail when location authorization is denied.
DO: Detect .denied status and show an alert with a Settings deep link.
DON'T: Assume geocoding always succeeds — handle empty results and network errors.
Review Checklist
References
- references/mapkit-patterns.md — Map setup, annotations, search, routes, clustering, Look Around, snapshots.
- references/mapkit-corelocation-patterns.md — CLLocationUpdate, CLMonitor, CLServiceSession, background location, testing.
- Core implementation details -- setup, API wiring, and focused implementation recipes moved out of the entrypoint.
1---2name: mapkit3description: Build or review maps and location features with MapKit and Core Location. Use for map views, annotations, overlays, user location, geocoding, place search, directions, geofencing, region monitoring, location authorization, or coordinate-based app behavior.4---56# MapKit78Build map-based and location-aware features targeting iOS 17+ with SwiftUI9MapKit and modern CoreLocation async APIs. Use `Map` with `MapContentBuilder`10for views, `CLLocationUpdate.liveUpdates()` for streaming location, and11`CLMonitor` for geofencing.1213Read [references/mapkit-patterns.md](references/mapkit-patterns.md) when you need full map setup, search,14routes, Look Around, snapshots, or iOS 26 place APIs. Read15[references/mapkit-corelocation-patterns.md](references/mapkit-corelocation-patterns.md) when the task involves16location update lifecycle, geofencing, background location, testing, or privacy keys.1718## Contents1920- [Workflow](#workflow)21- [Route by Task](#route-by-task)22- [Core Decisions](#core-decisions)23- [Common Mistakes](#common-mistakes)24- [Review Checklist](#review-checklist)25- [References](#references)2627## Workflow28291. Define the exact map/location capability and request only the authorization it needs.302. Choose SwiftUI Map or UIKit hosting, then establish camera ownership, selection, annotations, and overlay identity.313. Keep search, geocoding, directions, and live-location tasks cancellable and scoped to the current query/session.324. Handle denied/restricted/reduced-accuracy states and avoid treating coordinates as always available.335. Verify empty results, movement, camera changes, route failures, localization, offline/network transitions, and background rules.3435## Route by Task3637- Read [core implementation details](references/core-implementation.md) for SwiftUI maps, camera, annotations, overlays, live updates, authorization, geocoding, search, and directions.38- Read [MapKit patterns](references/mapkit-patterns.md) for clustering, Look Around, snapshots, complex routes, and full map setup.39- Read [Core Location patterns](references/mapkit-corelocation-patterns.md) for `CLLocationUpdate`, monitoring, service sessions, background behavior, and testing.4041## Core Decisions4243- Do not infer authorization from API availability or a non-nil cached location.44- Use stable identities for annotations and overlays.45- Debounce and cancel search/geocoding work when input changes.46- Treat size, viewport, and user interaction as live state rather than fixed screen assumptions.4748## Common Mistakes4950**DON'T:** Request always authorization upfront.51**DO:** Start with when-in-use authorization. On iOS 18+, hold a `CLServiceSession`52for the feature lifetime; request `.always` only for background features that53need system relaunch after termination.5455**DON'T:** Use `CLLocationManagerDelegate` for simple location fetches on iOS 17+.56**DO:** Use `CLLocationUpdate.liveUpdates()` async stream for cleaner, more concise code.5758**DON'T:** Ignore `CLLocationUpdate` diagnostics such as denied, globally denied, or unavailable location.59**DO:** Stop or degrade the feature, show recovery UI such as Settings guidance, and keep search/manual flows usable.6061**DON'T:** Let `liveUpdates()` run from an unowned task after the map/view is gone.62**DO:** Store the `Task`, cancel it when the feature stops, and filter invalid, inaccurate, stale, or impossible movement fixes.6364**DON'T:** Force-unwrap `CLPlacemark` properties — they are all optional.65**DO:** Use nil-coalescing: `placemark.locality ?? "Unknown"`.6667**DON'T:** Fire `MKLocalSearchCompleter` queries on every keystroke.68**DO:** Debounce with `.task(id: searchText)` + `Task.sleep(for: .milliseconds(300))`.6970**DON'T:** Silently fail when location authorization is denied.71**DO:** Detect `.denied` status and show an alert with a Settings deep link.7273**DON'T:** Assume geocoding always succeeds — handle empty results and network errors.7475## Review Checklist7677- [ ] Info.plist has `NSLocationWhenInUseUsageDescription` with specific reason78- [ ] Authorization denial handled with Settings deep link79- [ ] `CLLocationUpdate` task cancelled when not needed (battery)80- [ ] Location accuracy appropriate for the use case81- [ ] Map annotations use `Identifiable` data with stable IDs82- [ ] Geocoding errors handled (network failure, no results)83- [ ] Search completer input debounced84- [ ] `CLMonitor` limited to 20 conditions, instance kept alive85- [ ] Background location uses `CLBackgroundActivitySession`86- [ ] Map tested with VoiceOver87- [ ] Map annotation view models and location UI updates are `@MainActor`-isolated8889## References9091- [references/mapkit-patterns.md](references/mapkit-patterns.md) — Map setup, annotations, search, routes, clustering, Look Around, snapshots.92- [references/mapkit-corelocation-patterns.md](references/mapkit-corelocation-patterns.md) — CLLocationUpdate, CLMonitor, CLServiceSession, background location, testing.93- [Core implementation details](references/core-implementation.md) -- setup, API wiring, and focused implementation recipes moved out of the entrypoint.