Geolocation and Maps in Swift (iOS/macOS): Core Location and MapKit
Developing modern applications for iOS and macOS often requires working with geolocation and maps. In the Apple ecosystem, there are two powerful frameworks for these purposes: Core Location (obtaining geographic data) and MapKit (displaying maps, annotations, routes). In this article, we will thoroughly examine their capabilities, provide practical examples in Swift, and show how to integrate geolocation into your application.
1. Core Location: Determining Location
Core Location is a framework for obtaining geographic information: coordinates, altitude above sea level, direction of movement, as well as for monitoring geofences. It works with GPS, Wi-Fi, cellular towers, and iBeacon.
1.1. Setting Up Permissions
To use geolocation, you must request permission from the user. In the Info.plist file, add one of the keys:
NSLocationWhenInUseUsageDescription— to obtain data only when the app is actively in use.NSLocationAlwaysAndWhenInUseUsageDescription— for background monitoring (requires additional explanation).
1.2. Getting Current Coordinates
import CoreLocation
class LocationManager: NSObject, CLLocationManagerDelegate { private let manager = CLLocationManager() var onLocationUpdate: ((CLLocation) -> Void)?
override init() { super.init() manager.delegate = self manager.desiredAccuracy = kCLLocationAccuracyBest }
func requestLocation() { manager.requestWhenInUseAuthorization() manager.startUpdatingLocation() }
// MARK: - CLLocationManagerDelegate func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { guard let location = locations.last else { return } onLocationUpdate?(location) manager.stopUpdatingLocation() // Stop updates if only one value is needed }
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) { print("Error getting location: \\(error.localizedDescription)") }}
// Usagelet locationManager = LocationManager()locationManager.onLocationUpdate = { location in print("Latitude: \\(location.coordinate.latitude), Longitude: \\(location.coordinate.longitude)")}locationManager.requestLocation()2. MapKit: Displaying Maps and Annotations
MapKit allows you to embed interactive Apple Maps into applications. The main class is MKMapView.
2.1. Creating a Map in Storyboard/XIB
Add an MKMapView to the scene and create an IBOutlet in the controller. Make sure MapKit is imported.
2.2. Programmatically Creating a Map and Adding an Annotation
import MapKit
class MapViewController: UIViewController { var mapView: MKMapView!
override func viewDidLoad() { super.viewDidLoad() setupMap() addAnnotation() }
private func setupMap() { mapView = MKMapView(frame: view.bounds) mapView.autoresizingMask = [.flexibleWidth, .flexibleHeight] view.addSubview(mapView)
// Set region (Moscow) let center = CLLocationCoordinate2D(latitude: 55.751244, longitude: 37.618423) let span = MKCoordinateSpan(latitudeDelta: 0.05, longitudeDelta: 0.05) let region = MKCoordinateRegion(center: center, span: span) mapView.setRegion(region, animated: true) }
private func addAnnotation() { let annotation = MKPointAnnotation() annotation.coordinate = CLLocationCoordinate2D(latitude: 55.751244, longitude: 37.618423) annotation.title = "Moscow" annotation.s